Home | Forums | Articles | F@H
Help | Search | Members | Calendar | Forum Map | Cybervillage | Archive
Welcome Guest ( Log In | Register | Resend Validation Email )


>  IceTeks Forums -> Technology made cool -> Programming


  Archived - No replies allowed - start new thread insteadStart new topicStart Poll

>  21 Card Game help
Track this topic | Email this topic | Print this topic
DemonKing19
Posted: Mar 29 2007, 05:00 PM
Quote Post

Icicle

Group Icon

Group: IceTeks Newbies
Posts: 5
Member No.: 2 100
Joined: 29-March 07
Ice Cubes: 27



I've been working on a 21 card game for awhile, but I can't get it to work properly! It determines the winner fine but it outputs different totals then it displays in the last line, code is below. If anyone can help fix it up- its due tomorrow sad.gif -that would be great!

CODE

#include <iostream.h>
#include <H:/lvp/random.h>

int main()
{
// Variables
int numcard;
int youtotal;
int comptotal;
cout<<"Enter the number of cards you want: ";
cin>>numcard;
cout<<"You: ";
randomize;
for (int i=1; i<=numcard; i++)
 cout<<random(10);
youtotal=random(10);
cout<<endl;
cout<<"Computer: ";
for (int a=1; a<=numcard; a++)
 cout<<random(10);
comptotal=random(10);
cout<<endl;
//Detemine Winner and totals for you and the computer
cout<<"You have a total of ";
cout<<youtotal;
cout<<" and the computer has a total of ";
cout<<comptotal;
cout<<" therefore";
if (youtotal<21 && youtotal>comptotal) {
 cout<<" you win."<<endl;
}
else if (comptotal<21 && comptotal>youtotal) {
 cout<<" the computer  wins."<<endl;
}
else {
 cout<<"it is a draw."<<endl;
}
return(0);
}
PMEmail Poster Top
syb
Posted: Mar 30 2007, 08:50 AM
Quote Post

Liquid Nitrogen Blaster

Group Icon

Group: Ice Age Members
Posts: 231
Member No.: 137
Joined: 18-June 03
Ice Cubes: 27



It would kinda help to tell us what lingo your programing in.


--------------------
The wisdom of sight comes from the father of lights
PMEmail PosterMSN Top
Furball
Posted: Mar 30 2007, 11:35 AM
Quote Post

Beware of Squirrel

Group Icon

Group: Ice Age Members
Posts: 1 435
Member No.: 537
Joined: 30-September 04
Ice Cubes: 106627



QUOTE (syb @ Mar 30 2007, 08:50 AM)
It would kinda help to tell us what lingo your programing in.

Looks to be c++ on account of the cout statements.


--------------------
No, not the MASTER SWORD!

You Don't want your ring to get dirty now do you BILLLAYYY?!!

Balance of Traaaaaaaaadeees and Balance of Payyyyymennnts

That's right ladies, I gm'd spirit speak in a day ;)

OMG, Ryan you got 5 fleshrenderers after you, you're so screwed.
PMEmail Poster Top
DemonKing19
Posted: Mar 30 2007, 11:58 AM
Quote Post

Icicle

Group Icon

Group: IceTeks Newbies
Posts: 5
Member No.: 2 100
Joined: 29-March 07
Ice Cubes: 27



Yes, its C++, I thought it was in there, sorry! Any ideas anyone?
PMEmail Poster Top
manadren
Posted: Mar 30 2007, 07:09 PM
Quote Post

ph33r t3h kr4k3n

Group Icon

Group: Crunchers
Posts: 1 834
Member No.: 72
Joined: 1-January 03
Ice Cubes: 4392073



been a while since I've done any C programming but from what I can see, the value stored in youtotal is not the number displayed.


CODE
for (int i=1; i<=numcard; i++)
cout<<random(10);


here you generate the numbers for the cards, which are shown to the user, but the values aren't actually stored anywhere.


the next line

CODE

youtotal=random(10)


stores a different random number for the user total.

What you want to do is create another integer variable to store the random number in. Within the for loop you want to store the random number in that new variable, display the variable, then add it to youtotal.

For Example:

CODE
// Variables
int numcard = 0;
int youtotal = 0;
int comptotal = 0;
int cardvalue = 0;


(it's a good idea to set your variables to 0 or null when you declare them, particularly in C\C++)

and then further down where you calculate the card values

CODE

for (int i=1; i<=numcard; i++) {
cardvalue = random(10);
cout << cardvalue;
youtotal = youtotal + cardvalue;
}


--------------------
mana's blog {1, 2} yell at me when I get lazy
PMEmail PosterUsers Website Top
MikeDB
Posted: Apr 4 2007, 03:29 PM
Quote Post

Use the teeth!

Group Icon

Group: Ice Age Members
Posts: 1 248
Member No.: 874
Joined: 13-May 05
Ice Cubes: 95



I like pie!


--------------------
PMEmail PosterUsers WebsiteMSN Top
0 User(s) are reading this topic (0 Guests and 0 Anonymous Users)
0 Members:
« Next Oldest | Programming | Next Newest »

Topic Options Archived - No replies allowed - start new thread insteadStart new topicStart Poll

 



[ Script Execution time: 0.0532 ]   [ 13 queries used ]   [ GZIP Enabled ]

< Home | Forums | Contact >