Welcome to Anything Forums! We are a general discussion forum that believes in freedom of expression and aim to provide a low moderation (within reasonable means) environment to discuss any topic. If you want to join, simply click HERE to be taken to our account management system where you can make an account. If you had an account at iceteks.com, anythingforums.com, uovalor.com or uogateway.com in the past it has been transfered over and you can simply do a password reset.
Computer builds, hardware and software discussion or troubleshooting, including peripherals. Essentially a general place to talk about desktop computers.
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!
#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]
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]