Oct 1, 2009 at 4:31pm UTC
Hi, I have problems with my game:
If you deposit i.e.500 and bet 300. The remaining balance always says 0 when I in this case want it to output 200 and if you win or lose the game the balance is suposed to be updated as you play so you can use it(the sum you maybe win) when you bet next time.
My code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int bet_money;
int deposit;
bool loop1 = true ;
bool loop2 = true ;
bool loop3 = true ;
char end;
int diceone;
int dicetwo;
int dicethree;
int dicefour;
int points_user = 0;
int points_computer = 0;
int balance;
cout << "Welcome to the Dice Game!" << endl;
cout << "Do you want to play or exit the game?" << endl;
cout << "Enter e to exit and p to play" << endl;
cin >> end;
if (end == 'p' ) {
loop3 = true ;
} else if (end == 'e' ) {
loop3 = false ;
cout << "Game over" << endl;
} else {
loop3 = false ;
}
while (loop3) {
cout << "How much money do you want to deposit to your game-account?" << endl;
cin >> deposit;
cout << "You have deposited " << deposit << endl;
cout << "You can choose to bet 500SEK, 300SEK or 100 SEK. The first one to win two rounds out of three is the winner and will win the other player's bet cash" << endl; //istruktioner
loop1 = true ;
while (loop1) {
cout << "How much money do you want to bet?" << endl;
cin >> bet_money;
if ((bet_money == 500) || (bet_money == 300) || (bet_money == 100)) {
loop1 = false ;
cout << "Bet accepted " << endl;
} else {
loop1 = true ;
cout << "Entered bet is not an allowed value" << endl;
}
}
cout << "You have bet " << bet_money << endl;
balance + (deposit - bet_money);
cout << "Your balance is now " << balance << endl;
loop2 = true ;
points_user = 0;
points_computer = 0;
while (loop2) {
cout << "Press ENTER to throw your dice" << endl;
cin.get();
cin.get();
srand(time(0));
diceone = rand() % 6 + 1;
cout << diceone << endl;
cout << "Press ENTER to throw your second dice" << endl;
cin.get();
dicetwo = rand() % 6 + 1;
cout << dicetwo << endl;
cout << "Your total amount is:" << dicetwo + diceone << endl;
cout << "Press ENTER to let the computer throw its dice" << endl;
cin.get();
srand(time(0));
dicethree = rand() % 6 + 1;
cout << dicethree << endl;
cout << "Press ENTER to let the computer throw its second dice" << endl;
cin.get();
dicefour = rand() % 6 + 1;
cout << dicefour << endl;
cout << "Total amount for the computer is:" << dicethree + dicefour << endl;
if ((dicethree + dicefour) < (dicetwo + diceone)) {
cout << "You won" << endl;
points_user++;
} else if ((dicethree + dicefour) > (dicetwo + diceone)) {
cout << "You lost" << endl;
points_computer++;
} else {
cout << "Tie" << endl;
}
cout << "Scores are: User-Computer" << endl;
cout << points_user;
cout << "-" ;
cout << points_computer << endl;
if (points_computer == 2) {
loop2 = false ;
cout << "You lost" << endl;
cout << "Your balance is now" << endl;
cout << balance;
cout << "Enter e to exit and p to play" << endl;
cin >> end;
if (end == 'p' ) {
loop3 = true ;
} else if (end == 'e' ) {
loop3 = false ;
cout << "Game over" << endl;
} else {
loop3 = false ;
}
} else if (points_user == 2) {
loop2 = false ;
cout << "You won the game and a total of" << endl;
cout << bet_money * 2 << endl;
cout << "Your balance is now :" << endl;
cout << (balance + (bet_money * 2)) << endl;
cout << "Enter e to exit and p to play" << endl;
cin >> end;
if (end == 'p' ) {
loop3 = true ;
loop1 = true ;
} else if (end == 'e' ) {
loop3 = false ;
cout << "Game over" << endl;
} else {
loop3 = false ;
}
} else {
loop2 = true ;
}
cin.ignore();
}
}
}
Last edited on Oct 2, 2009 at 3:18pm UTC
Oct 1, 2009 at 4:40pm UTC
Please you code tags in future - the '#' sign to the right ->
This line is wrong: balance + (deposit - bet_money);
Oct 1, 2009 at 4:44pm UTC
and how do I correct that line in a proper way?
Last edited on Oct 1, 2009 at 4:47pm UTC
Oct 1, 2009 at 7:54pm UTC
You never assign anything to balance as far as I've looked. That's why balance never changes.
Oct 1, 2009 at 8:38pm UTC
okay, I want (deposit - bet_money) to be added to balance and how can I assign that correctly? Do you understand what I wanna achieve? I don't really know how to do the correct syntax can you help me out?
I could just type
balance = (deposit - bet_money);
But then it will be wrong the next time the loop is run...
Last edited on Oct 2, 2009 at 6:50am UTC
Oct 2, 2009 at 2:58pm UTC
Why would it be wrong? Sorry, your code is hard to view when it is not properly indented and outside of the code tages.
Oct 2, 2009 at 3:40pm UTC
beat me to it
Last edited on Oct 2, 2009 at 3:41pm UTC
Oct 2, 2009 at 3:48pm UTC
okay, thanks that helped my out a bit. The only thing that gets wrong now is when I play for the third time the balance gets wrong again.
http://img89.imageshack.us/img89/7874/27761365.jpg
As you see in the image the window says balance is 400 at the end when it is suposed to be
1000 (1000 left from previous games and 500 deposited=1500 and then-500 for the bet=1000).
my code looks like this now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int bet_money;
int deposit;
bool loop1 = true ;
bool loop2 = true ;
bool loop3 = true ;
char end;
int diceone;
int dicetwo;
int dicethree;
int dicefour;
int points_user = 0;
int points_computer = 0;
int balance;
cout << "Welcome to the Dice Game!" << endl;
cout << "Do you want to play or exit the game?" << endl;
cout << "Enter e to exit and p to play" << endl;
cin >> end;
if (end == 'p' ) {
loop3 = true ;
} else if (end == 'e' ) {
loop3 = false ;
cout << "Game over" << endl;
} else {
loop3 = false ;
}
while (loop3) {
cout << "How much money do you want to deposit to your game-account?" << endl;
cin >> deposit;
balance + deposit;
cout << "You have deposited " << deposit << endl;
cout << "You can choose to bet 500SEK, 300SEK or 100 SEK. The first one to win two rounds out of three is the winner and will win the other player's bet cash" << endl; //istruktioner
loop1 = true ;
while (loop1) {
cout << "How much money do you want to bet?" << endl;
cin >> bet_money;
if ((bet_money == 500) || (bet_money == 300) || (bet_money == 100)) {
loop1 = false ;
cout << "Bet accepted " << endl;
} else {
loop1 = true ;
cout << "Entered bet is not an allowed value" << endl;
}
}
cout << "You have bet " << bet_money << endl;
balance += (deposit - bet_money);
cout << "Your balance is now " << balance << endl;
loop2 = true ;
points_user = 0;
points_computer = 0;
while (loop2) {
cout << "Press ENTER to throw your dice" << endl;
cin.get();
cin.get();
srand(time(0));
diceone = rand() % 6 + 1;
cout << diceone << endl;
cout << "Press ENTER to throw your second dice" << endl;
cin.get();
dicetwo = rand() % 6 + 1;
cout << dicetwo << endl;
cout << "Your total amount is:" << dicetwo + diceone << endl;
cout << "Press ENTER to let the computer throw its dice" << endl;
cin.get();
srand(time(0));
dicethree = rand() % 6 + 1;
cout << dicethree << endl;
cout << "Press ENTER to let the computer throw its second dice" << endl;
cin.get();
dicefour = rand() % 6 + 1;
cout << dicefour << endl;
cout << "Total amount for the computer is:" << dicethree + dicefour << endl;
if ((dicethree + dicefour) < (dicetwo + diceone)) {
cout << "You won" << endl;
points_user++;
} else if ((dicethree + dicefour) > (dicetwo + diceone)) {
cout << "You lost" << endl;
points_computer++;
} else {
cout << "Tie" << endl;
}
cout << "Scores are: User-Computer" << endl;
cout << points_user;
cout << "-" ;
cout << points_computer << endl;
if (points_computer == 2) {
loop2 = false ;
cout << "You lost" << endl;
cout << "Your balance is now" << endl;
cout << balance << endl;
cout << "Enter e to exit and p to play" << endl;
cin >> end;
if (end == 'p' ) {
loop3 = true ;
} else if (end == 'e' ) {
loop3 = false ;
cout << "Game over" << endl;
} else {
loop3 = false ;
}
} else if (points_user == 2) {
loop2 = false ;
cout << "You won the game and a total of" << endl;
cout << bet_money * 2 << endl;
cout << "Your balance is now :" << endl;
cout << (balance + (bet_money * 2)) << endl;
cout << "Enter e to exit and p to play" << endl;
cin >> end;
if (end == 'p' ) {
loop3 = true ;
loop1 = true ;
} else if (end == 'e' ) {
loop3 = false ;
cout << "Game over" << endl;
} else {
loop3 = false ;
}
} else {
loop2 = true ;
}
cin.ignore();
}
}
}
Last edited on Oct 2, 2009 at 3:51pm UTC
Oct 2, 2009 at 5:30pm UTC
Line 36 doesn't do anything. You're adding balance and deposit, but you aren't assigning the result to anything.
Before you enter your first loop, initialize balance to zero: balance = 0;
Then replace line 36 with balance += deposit;
In your 'win' code, you don't actually add the winnings back into balance. You have this line:
cout << (balance + (bet_money * 2)) << endl;
That outputs the sum to the console, but this doesn't assign the sum back to your balance.
You should have a line balance += bet_money*2;
, then change you output command to only output the balance value.
You also only need to seed srand() once at the beginning of the program. There's no need to seed it every time you use rand().
Oct 2, 2009 at 6:19pm UTC
okay thanks alot, I dont just understand where to put: balance = 0;
I tried to put it before line 33 and before line 40 and none did work.
Oct 2, 2009 at 6:33pm UTC
I should have recommended this previously instead of a separate initialization, but initialize balance when you declare it. Line 19: int balance = 0;
Oct 2, 2009 at 7:03pm UTC
thanks a lot for your help I really appreciate it. Can you have a look at line 54 is it correct? Because if I deposit 500 and bet 300 it says that my balance is 700 when it should be 200. I have corrected everything you told me to.
EDIT: line 55
Last edited on Oct 2, 2009 at 8:00pm UTC