[Site Home] [Forum Home] [Articles] [File DB] [News Archives]

Programming -> 21 Card Game help


(View original topic)


MikeDB - Apr-04-2007 server time
I like pie!

manadren - Mar-30-2007 server time
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;
}

DemonKing19 - Mar-30-2007 server time
Yes, its C++, I thought it was in there, sorry! Any ideas anyone?

Furball - Mar-30-2007 server time
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.

syb - Mar-30-2007 server time
It would kinda help to tell us what lingo your programing in.

DemonKing19 - Mar-29-2007 server time
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);
}

(Showing 50 last posts, newest on top)