-3

I'm building a simple dice game in C++ where you play against the computer, before the game you can bet lets say 100, if you win you should win the double of that = 200, and if u lose, 100 will be withdrawn from ur account that you have

I have these variables:

int bet = 0; 
int account = 0

and Im trying to make what I told about up there with this:

if (computer > rounds)
   {
    wcout<< "Im sorry the computer won this round you have this amount left on your account:" << account - bet << endl;
   }
   else if (player > rounds)
   {
    wcout<< "Gratz you won this round now you have:" << account + bet*2 << endl;
   }

It's not working out and I've been trying to figure out why, any help is appreciated!

Dingredient
  • 2,191
  • 22
  • 47
Anden120
  • 3
  • 5

2 Answers2

2

You're computing the value, but not storing it anywhere. You're looking for this:

if (computer > rounds)
   {
    account -= bet;
    wcout<< "Im sorry the computer won this round you have this amount left on your account:" << account << endl;
   }
   else if (player > rounds)
   {
    account += 2 * bet;
    wcout<< "Gratz you won this round now you have:" << endl;
   }

Seeing as this is a rather basic question, you might want to consider picking up a good book.

Community
  • 1
  • 1
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • Thanks, and thanks for the linky! Does it save the value of the variabel if Im using a while loop to restart the game? – Anden120 Oct 07 '13 at 19:53
  • @Anden120 A variable's value is preserved during its lifetime. If the variables are defined outside of the loop's body, they will be preserved. If they're defined inside the body, each iteration gets its own specimen of the variables, so their values will not be preserved. BTW, if the answer solves your problem, you should mark it as accepted (green tick-mark, only one per question). That's how SO works. – Angew is no longer proud of SO Oct 07 '13 at 20:17
0

Note that the following line:

wcout<< "Gratz you won this round now you have:" << account + bet*2 << endl;

doesn't modify account variable, just reads and uses its value so that account + bet*2 can be displayed. In case this is placed within a loop and you'd like to modify the account, you should do:

acount += bet * 2;
wcout<< "Gratz you won this round now you have:" << account << endl;
LihO
  • 41,190
  • 11
  • 99
  • 167