21 Card Game help

Computer builds, hardware and software discussion or troubleshooting, including peripherals. Essentially a general place to talk about desktop computers.
Locked
Anonymous

21 Card Game help

Post 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]
syb
Posts: 222
Joined: Wed Jun 18, 2003 10:12 pm

21 Card Game help

Post 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
The wisdom of sight comes from the father of lights
User avatar
Death
Posts: 7919
Joined: Thu Sep 30, 2004 10:12 pm

21 Card Game help

Post 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
Anonymous

21 Card Game help

Post 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
User avatar
manadren_it
Posts: 1810
Joined: Wed Jan 01, 2003 6:48 pm

21 Card Game help

Post 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]
mana's blog {1, 2} yell at me when I get lazy
User avatar
MikeDB
Posts: 1224
Joined: Fri May 13, 2005 3:09 pm

21 Card Game help

Post by MikeDB »

I like pie!

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