While loop in C++

Soldato
Joined
14 Apr 2004
Posts
11,888
Location
UK
Hey guys,

Making a One Arm Bandit Machine and the program is almost done (Or we assume so!) bar this section:

PHP:
void chooseOptions()
{
 if (usersOption == 'R' || usersOption ==  'r')
  displayRules();
 else if (usersOption == 'P' || usersOption ==  'p')
  playOneGame();
 else if (usersOption == 'B' || usersOption ==  'b')
  displayResults();
}

We're trying to achieve a main page that directs everything to sub menu's.

Been trying Trial and Error for a stupid amount of hours over the past 6 days and we're having no luck :(

Time is running out, have 4 hours till deadline :(

Here's an example:

etendedzw4.jpg


Would appreciate any help please!
 
What exactly is the problem with it?

Just having a quick look, shouldn't it be
Code:
usersOption == "R"
instead of
Code:
usersOption == 'R'

and the same for the other characters.
 
int main ()
{

cout <<"\n";
cout <<"\n";
cout <<"\n";
cout << "----------AOB OPTIONS----------"<< '\n';
cout << "DISPLAY THE RULES: R"<< '\n';
cout << "PLAY ONE GAME: P"<< '\n';
cout << "DISPLAY BALANCE: B"<< '\n';
cout << "SHOW STATISTICS: S"<< '\n';
cout << "SET TESTING MODE: T"<< '\n';
cout << "QUIT PROGRAM: Q"<< '\n';
cout << "-----------------------------"<< '\n';
cout << "ENTER YOUR COMMAND (R, P, S, T or Q):"<< '\n';
cout << "-----------------------------"<< '\n';
cin >> usersOption;
cout << "-----------------------------"<< '\n';
cout <<"\n";
cout <<"\n";
chooseOptions();


return (0);

}
 
Is usersOption a global variable? Because if it is only a local variable, then you will need to pass it to the funtion chooseOptions?

(unless I'm getting mixed up between C and C++)

Try displaying usersOption right after it has been entered to check it has been entered correctly, and also display it in your function right before you do the comparisons.

Sorry I can't offer any other advice, and good luck :)
 
Why don't you just pass usersOptions to chooseOptions() as an argument?

I tend to printf or cout variables just before I do tests on them to see what the compiler thinks the variables are before I test them. You'd be surprised how often you miss something daft.
 
Are you at Sheff Hallam? That looks similar to something I did in the first year (changed course now though so can't help you).
 
jamiemoles said:
Why don't you just pass usersOptions to chooseOptions() as an argument?

I tend to printf or cout variables just before I do tests on them to see what the compiler thinks the variables are before I test them. You'd be surprised how often you miss something daft.
Could you please elaborate of that? Especially the arguments?

Tommy - I am.
 
The following code might work - please bear in mind I'm a little rusty!

void chooseOptions(int test) // or char test
{
if (test == 'R' || test == 'r')
displayRules();
else if (test == 'P' || test == 'p')
playOneGame();
else if (test == 'B' || test == 'b')
displayResults();
}


int main ()
{

cout <<"\n";
cout <<"\n";
cout <<"\n";
cout << "----------AOB OPTIONS----------"<< '\n';
cout << "DISPLAY THE RULES: R"<< '\n';
cout << "PLAY ONE GAME: P"<< '\n';
cout << "DISPLAY BALANCE: B"<< '\n';
cout << "SHOW STATISTICS: S"<< '\n';
cout << "SET TESTING MODE: T"<< '\n';
cout << "QUIT PROGRAM: Q"<< '\n';
cout << "-----------------------------"<< '\n';
cout << "ENTER YOUR COMMAND (R, P, S, T or Q):"<< '\n';
cout << "-----------------------------"<< '\n';
cin >> usersOption;
cout << "-----------------------------"<< '\n';
cout <<"\n";
cout <<"\n";
chooseOptions(usersOption);


return (0);

}
 
Just think about it logically - you want to keep displaying the menu until an option is picked or the user wants to quit... that suggests a looping structure.

so...

Code:
START LOOP
   print menu
   capture user input
   call function chosen by user
   test for quit
END LOOP

Try using a switch statement instead of lots of if .. else if.

Convert your user input to lower case before you do the test, then you wont have to check for both cases.

PS - congratulations on leaving it until hand-in day (was an expert at this myself during uni days!)
 
Jamiemoles- Tried that and messing around with it for almost an hour, still no luck.

LazyManc - In all honestly, time was an enemy against us, we had 4 other assignment prior to this!

Got 90 minutes or so left, this doesnt look good :(
 
Right, cout the usersOption variable before entering the chooseOption function, then do it again as the first line of the chooseOption function.

What do you get?


Also what datatype have you defined usersOption as?
 
Last edited:
why can't you trace through in the debugger so you can watch the variables and see if it falls into the if statements
 
jamiemoles said:
Right, cout the usersOption variable before entering the chooseOption function, then do it again as the first line of the chooseOption function.

What do you get?


Also what datatype have you defined usersOption as?
We get exactly the same problem and the usersOption is defined as int.
 
LazyManc said:
No wonder it doesn't work, you're testing an integer against a char!

:D

My thoughts exactly. Unfortunately a lot of the older C and C++ compilers used to let you do this.

Try redefining the variable as char and see what happens. Also check those quotes around the options.
 
It's been a while since I've done any C++ but just found this from my uni days (which coincidently was from a fruit machine app!).

Code:
int main()
{
	
	Exit = false;  // set exit flag to false
	for (;;) // loop until break
	{
		Choice = DrawMenu();
		switch(Choice)
		{
			case ('1'):
				{
					// do option 1
				}
				break;
			case ('2'):
				{
					// do option 2
				}
				break;
			case ('q'):
				{
					cout << "\n Goodbye, and thanks for playing \n";
					Exit=true;
					break;
				}
			default:
				{
					system("cls"); // clear the screen
					cout << "Your choice was not recognised\n"; // if user gives incorrect input
					break;
				}
		}

		if (Exit)
			break; // end forever loop
	}		
}


/* Draws the menu screen and takes in the user input
*/
char DrawMenu()
{
	char MenuChoice;
	cout << "\n**** Menu ****\n";
	cout << "1. Option 1\n";
	cout << "2. Option 2\n";
	cout << "Q. Quit\n\n";
	cout << ": ";
	cin >> MenuChoice;  // get user input
	return MenuChoice;
}

Had to strip out the OO stuff but it should work with a little effort.
 
Back
Top Bottom