Page 1 of 1

21 Card Game help

Posted: Thu Mar 29, 2007 5:00 pm
by Anonymous
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 :( -that would be great!

Code: Select all

#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);
}[code] 

[color=#888888][size=85]Archived topic from Iceteks,  old topic ID:4765, old post ID:37654[/size][/color]

21 Card Game help

Posted: Fri Mar 30, 2007 8:50 am
by syb
It would kinda help to tell us what lingo your programing in.

Archived topic from Iceteks, old topic ID:4765, old post ID:37657

21 Card Game help

Posted: Fri Mar 30, 2007 11:35 am
by Death
syb wrote: It would kinda help to tell us what lingo your programing in.
Looks to be c++ on account of the cout statements.

Archived topic from Iceteks, old topic ID:4765, old post ID:37658

21 Card Game help

Posted: Fri Mar 30, 2007 11:58 am
by Anonymous
Yes, its C++, I thought it was in there, sorry! Any ideas anyone?

Archived topic from Iceteks, old topic ID:4765, old post ID:37659

21 Card Game help

Posted: Fri Mar 30, 2007 7:09 pm
by manadren_it
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: Select all

for (int i=1; i<=numcard; i++)
 cout<<random(10);
[code]

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)[code]

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;
[code]

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

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;
}
[code] 

[color=#888888][size=85]Archived topic from Iceteks,  old topic ID:4765, old post ID:37662[/size][/color]

21 Card Game help

Posted: Wed Apr 04, 2007 3:29 pm
by MikeDB
I like pie!

Archived topic from Iceteks, old topic ID:4765, old post ID:37677