Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

-Help with some code-


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 afmack   Members   -  Reputation: 111

Like
0Likes
Like

Posted 23 June 2012 - 03:18 AM

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 <iostream>
#include <cstdlib>
#include <ctime>
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;
}

Edited by afmack, 23 June 2012 - 03:31 AM.


Sponsor:

#2 afmack   Members   -  Reputation: 111

Like
0Likes
Like

Posted 23 June 2012 - 03:21 AM

Code snippet wasn't cooperating. >.<

Edited by afmack, 23 June 2012 - 03:32 AM.


#3 Radikalizm   Members   -  Reputation: 2048

Like
0Likes
Like

Posted 23 June 2012 - 05:45 AM

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.

Edited by Radikalizm, 23 June 2012 - 05:47 AM.


#4 afmack   Members   -  Reputation: 111

Like
0Likes
Like

Posted 23 June 2012 - 08:53 AM

Thanks Rad. I knew I should just sleep and take a look at it again, lol. :P And noted on the srand. I'll be sure to change that.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS