Skip to main content
GameDev.net gamedev.net
🔒 Locked

-Help with some code-

Started by Spencer Allen McMillan Jun 23, 2012 at 9:18 AM 2 replies 1.3k views
Original Post
Spencer Allen McMillan
Spencer Allen McMillan
Hello, I'm workin' on a slot machine program and for some reason I'm having some issues with the menu. Selecting one breaks out of the menu loop and will allow you to play the slot machine game. Selecting two quits the game. And anything else it prints onto the console "Please enter a valid option." For some reason, when an invalid number is entered it prints but it doesn't loop back. So yeah, slightly confused. *I apologize, but the code snippet tool was not cooperating with me, only showing a portion of whatever code I put in*

Here's the code:


#include
#include
#include
using namespace std;

int Random( int, int );

int main()
{
//---- Initialization-------------------------------------------------
int low;
int high;
int chips = 1000;
int hold = 0;

bool quit = false;
//----End Init--------------------------------------------------------

// Start Game---------------------------------------------------------
while( !quit ){
//----Menu--------------------------------------------------------
while( hold <= 0 )
{
cout << "Player's chips: $" << chips << endl;
cout << "1) Play slot. 2) Exit." << endl;
cin >> hold;

if ( hold == 1 )
break;
if ( hold == 2 )
quit = true;
else
cout << "Please select a valid option" << endl;

}
//----End Menu----------------------------------------------------
}

cin.get();
cin.get();

return 0;
//---End Game---------------------------------------------------------
}

int Random( int low, int high )
{
srand(time(0));

int a = (rand() + low) % high;
return a;
}
Radikalizm
Radikalizm
You're defining your inner while loop as while(hold <= 0), this will be true for for the very first iteration since you're setting the value for hold to 0. However, after your cin call you read in a new value for hold and you run one iteration of your loop. If you were to enter in a value of <= 0 with your cin call your loop would continue on fine, but when you enter a value > 0 your inner loop condition returns false and your loop won't execute. You could possibly solve this by setting your hold variable back to 0 when someone enters an invalid value.
Also, you might want to try to rewrite those nested loops as a single loop, there's no need for a nested loop in this occasion, and it could potentially save you some headaches later on, but that's my opinion of course.

EDIT: Why are you doing random number generation like that? srand should be called only once at the beginning of your application, no need to seed your PRNG every time you generate a random number.
I gets all your texture budgets!
Spencer Allen McMillan
Spencer Allen McMillan
Thanks Rad. I knew I should just sleep and take a look at it again, lol. And noted on the srand. I'll be sure to change that.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.