I've stared at this forever, and I can't figure out why it isn't looping. Everything works fine, except it isn't looping. Maybe it's that I've been staring at it too long, and I need a new eye and keep overlooking it.
#include<iostream>
#include<ctime>
#include<string>
usingnamespace std;
int randa = 100;
int randb = 100;
int money = rand()%randb+randa;
int highlow ()
{
{
srand((unsigned)time(0));
int num = rand()%10;
int num2 = rand()%10;
int highlowbet;
string HLguess;
int payoff = 1;
cout<<"\nHIGH-LOW. The computer will generate a random number between 0 and 10. Choose whether or not the next number will be higher or lower than the first one.\n"
<<"I would like to bet $";
cin>>highlowbet;
//invalid entry
if (highlowbet < 1 || highlowbet > money)
{
cout<<"You can't bet that. Try again. What is your bet? $";
cin>>highlowbet; }
cout<<"The first number is "<<num<<". Think the number is higher or lower than "<<num<<"?\n";
cin>>HLguess;
if (HLguess == "higher" && num < num2){
cout<<"You're right! It was higher.";
cout<<"The number was "<<num2<<".\n--------------------\n\n";
money = money + payoff*highlowbet;
return(money); system("PAUSE");
}
elseif (HLguess == "higher" && num > num2){
cout<<"You're wrong. It was lower.";
cout<<"The number was "<<num2<<".\n--------------------\n\n";
money = money - highlowbet;
return(money);system("PAUSE");
}
elseif (HLguess == "lower" && num < num2){
cout<<"You're wrong. It was higher.";
cout<<"The number was "<<num2<<".\n--------------------\n\n";
money = money - highlowbet;
return(money);system("PAUSE");
}
elseif (HLguess == "lower" && num > num2){
cout<<"You're right! It was lower.";
cout<<"The number was "<<num2<<".\n--------------------\n\n";
money = money + payoff*highlowbet;
return(money);system("PAUSE");
}
return(money);system("PAUSE");
}
}
int roulette ()
{
cout<<"roulette";
return(2);
}
int blackjack ()
{
cout<<"blackjack";
return(3);
}
int slots()
{
cout<<"slots";
return(4);
}
int main()
{
srand((unsigned)time(0));
money = rand()%randb+randa;
cout<<"Welcome to the Eiselen Casino!\n\nYou have $"<<money<<" to gamble with.\n\nYou can play High-Low, Roulette, Blackjack, or Slots. You may play for how ever long you would like as long as you do not bust. \nGood luck!\n\n";
int choice;
do {
cout<<"Please choose which game you would like to play:\n"
<<"1) High low\n"
<<"2) Roulette\n"
<<"3) Blackjack\n"
<<"4) Slots\n"
<<"5) I want to leave\n\n";
cin>>choice;
switch (choice)
{
case 1:
if (choice == 1) {
int h;
h = highlow();
h;
cout<<"You now have $"<<h;
system("PAUSE");
break;
}
case 2:
if (choice == 2){
int r;
r = roulette();
r;
cout<<"You now have $"<<r;
system("PAUSE");break;
}
case 3:
if (choice == 3){
int b;
b = blackjack();
b;
cout<<"You now have $"<<b;
system("PAUSE");break;
}
case 4:
if (choice == 4) {
int s;
s = slots();
s;
cout<<"You now have $"<<s;
system("PAUSE");break;
}
case 5:
if (choice == 5){
cout<<"hi";
system("PAUSE");break;
}
}
} while (money < 0);
cout<<"You're bankrupt!";
return (0);
}
int main()
{
srand((unsigned)time(0));
money = rand()%randb+randa;
cout<<"Welcome to the Eiselen Casino!\n\nYou have $"<<money<<" to gamble with.\n\nYou can play High-Low, Roulette, Blackjack, or Slots. You may play for how ever long you would like as long as you do not bust. \nGood luck!\n\n";
int choice;
do {
cout<<"Please choose which game you would like to play:\n"
<<"1) High low\n"
<<"2) Roulette\n"
<<"3) Blackjack\n"
<<"4) Slots\n"
<<"5) I want to leave\n\n";
cin>>choice;
switch (choice)
{
case 1:
if (choice == 1) {
int h;
h = highlow();
h;
cout<<"You now have $"<<h;
system("PAUSE");
break;}
case 2:
if (choice == 2){
int r;
r = roulette();
r;
cout<<"You now have $"<<r;
system("PAUSE");break;
}
case 3:
if (choice == 3){
int b;
b = blackjack();
b;
cout<<"You now have $"<<b;
system("PAUSE");break;
}
case 4:
if (choice == 4) {
int s;
s = slots();
s;
cout<<"You now have $"<<s;
system("PAUSE");break;
}
case 5:
if (choice == 5){
return 0;break;
}
}
} while (choice == 5);
return (0);
system("PAUSE");
}
Common ways to figure this is out is to simply use debugging techniques. For small problems and programs like these, a simple cout statement or printf statement or even cerr redirected to a log file or the console would do just fine.
Do simple things like print choice variable after it's been set to the said input. Because choice is most likely not 5, the loop will fail for whatever reason.
Also, code like
1 2 3
int s;
s = slots();
s;
is EXTREMELY bad practice. This could be easily optimized and made more readable by doing int s = slots();. That and s; in your code is a waste of CPU since it does absolutely nothing.