slot machine. How do I get it to stop outputting negative number

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
const int NO_MATCH = 5;
const int TWO_MATCH = 1;
const int THREE_MATCH = 30;
int main ()
{
int choice;
int bank_amount = 0;
int game_amount = 0;
int transfer = 0;
int spins = 0;
char letter_a = 'a';
char letter_b = 'b';
char letter_c = 'c';
char letter_d = 'd';
int a_count;
int b_count;
int c_count;
int d_count;
int spin_count = 0;
int rand_letter;
int rand_letr_count;

srand(time(NULL));

cout<<"Welcome to Cletus's horrible Slots" <<endl; //Welcome user
cout<<" Chose one of the following: "<<endl; //User choice
cout<<"1.Check Balance"<<endl;
cout<<"2.Transfer Money"<<endl;
cout<<"3.Play the game"<<endl;
cout<<"4.Leave"<<endl;
cin>>choice;

bank_amount = 100* ( rand() % 10 + 2);

switch(choice)
{
case 1:
cout<<"Bank balance "<<bank_amount<<endl; //Bank Balance
cout<<"Game balance "<<game_amount<<endl; // Game Balance

case 2:
cout<<"How much money would you like to transfer (multiples of 100?)"
<<endl; //Transfer money to slot machine
cin>>transfer;
break;

transfer *= 100;
if (bank_amount >= transfer)
{
if(bank_amount > 5)
{
bank_amount = bank_amount + transfer;
game_amount = game_amount - transfer;
}
cout<<"Wow! You Suck game over!"<<endl;

cout<<"Please give me the amount you actually have."<<endl;
cin>> transfer;
}
case 3:
cout<<"How many times would you like to spin?"<<endl; //Number of spins
cin>>spins;

if(game_amount > (5 * spins))
{
cout<<"You are going to empty out your account."
"Pick a smaller amount of spins"<<endl;
cin>>spins;
break;
}
for(spin_count = spins; spin_count > 0; spin_count--)
{
a_count = 0;
b_count = 0;
c_count = 0;
d_count = 0;

cout<<"Your spin outcome: "<<endl;
for(rand_letr_count = 1; rand_letr_count < 4; rand_letr_count++)
{
rand_letter = rand() % 4 + 1;

if(rand_letter == 1)
{
a_count++;
cout<<letter_a;
}

else if(rand_letter == 2)
{
b_count++;
cout<<letter_b;
}

else if(rand_letter == 3)
{
c_count++;
cout<<letter_c;
}

else
{
d_count++;
cout<<letter_d;
}

}

if(a_count == 3 || b_count == 3 || c_count == 3 || d_count == 3)
{
cout<<" Bank Amount: "<<bank_amount<<endl;
cout<<" Game Amount: "<<game_amount<<endl;
cout<<"You win $30!!" <<endl;
game_amount += THREE_MATCH;



}

else if(a_count == 2 || b_count == 2 || c_count == 2 || d_count == 2)
{
cout<<" Bank Amount: "<<bank_amount<<endl;
cout<<" Game Amount: "<<game_amount<<endl;
cout<<"You Win $1 !!" <<endl;
game_amount += TWO_MATCH;

}
else
{
cout<<" Bank Amount: "<<bank_amount<<endl;
cout<<" Game Amount: "<<game_amount<<endl;
cout<<"LOSE $5!!" <<endl;
game_amount = game_amount - NO_MATCH;

}
}
case 4:
{
cout<<" Bank balance: "<<bank_amount<<endl;
cout<<" Game balance: "<<game_amount<<endl;
bank_amount += game_amount;
cout<<" Ending bank balance is: " <<bank_amount<<endl;
cout<<" Ending game balance is: "<<game_amount<<endl;
cout<<" Thank you for playing, have a great day."<<endl;
return 0;
}

}
}


You either do a sanity check for anything less than zero, or you switch to using unsigned integers. Where in your code are you trying to prevent the negative numbers from showing up?
Last edited on
I want to prevent the game amount from showing up negative
Last edited on
If the negative quantity is valid, you could change the sign to positive, but add the text "in debt" or "overdrawn" or similar.
I don't get why though. From what I understand of your code a negative value for 'game_amount' is perfectly valid. It indicates that the player has incurred a net loss playing the game.
closed account (E0p9LyTq)
You have two choices to prevent your numbers turning negative (as Computergeek01 pointed out):

1. At each step check your numbers are not negative, or could result in negative numbers (Bet value exceeding current balance for example)

2. Don't use signed variable such as int. Use unsigned variables. You still need to check for overflow conditions.

Both require you to redesign your code, which means work. Not just wishing something were to happen.

And PLEASE, learn to use code tags to make reading your posted source MUCH easier. http://www.cplusplus.com/articles/jEywvCM9/

You can edit your post to include code tags.
I tried many ways of doing this but there is a certain way I have to submit which is not what I'm getting.

seed the random number generator to ..... ummm. ..... 5, yeah 5.
choose option 3
choose option 1
choose option 2 and attempt to transfer $1200
choose option 2 and attempt to transfer -$300
choose option 2 transfer $200 to game balance
choose option 3 and request 300 spins
choose option 3 and request -5 spins
choose option 3, spin 40 times
choose option 3, spin 1 time
quit

so I might have some logic errors
Topic archived. No new replies allowed.