Do while menu

I'm trying to create a basic menu but I am having trouble where if the user inputs Q, it will terminate the program and execute like a statement saying "Terminating program" or something like that.

What is the while condition to make this?

This is what I have right now, and when I press Q, the menu will just keep resetting instead of terminating
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main()
{
	char choice;

	do
	{
		cout << "A) for A\nB) for B\nC) for C\nQ) Quit" << endl;
		cin >> choice;
		if (choice == 'a' || choice == 'A')
		{
			cout << "A" << endl;
		}
		else if (choice == 'b' || choice == 'B')
		{
			cout << "B" << endl;
		}
		if (choice == 'c' || choice == 'C')
		{
			cout << "C" << endl;
		}
	} while (choice == 'q' || choice == 'Q');
	cout << "Exit" << endl;
	system("PAUSE");
	return 0;
}
Read your while-loop condition more carefully. You're telling it to keep looping as long as the user enters quit. You literally want the opposite of that. Change it to -

while (choice != 'q' && choice != 'Q');
You should try using switch statements.
Ah woops my previous while condition was exactly like yours but instead of an AND truth i used an OR

why does this work:
while (choice != 'q' && choice != 'Q')

but this doesnt:
while (choice != 'q' || choice != 'Q')

because I get like if the user inputs A, then choice doesn't equal q which is true, so it will loop back, why do we have to use an AND truth?
The OR statement and the And statement works in this program. The only difference between the two is that the OR statement only requires 1 of them to be true before launching the code, where the AND statement requires both of them to be true to launch the code. But in most programs you see, very few ever really uses the DO-While loops, most use the While loops.
@Mort88

Sorry man but you are wrong, the OR statement does not work in this case

@Alex067

You would be right, could of swore I ran it with a OR statement with the program as is other then the Not. But I must of changed the test to make it work with a OR statement and forgot to include it my description.
Hi,

A few things here:

Make use of the tolower or toupper functions, so you only compare to one thing not two, so you halve the logic you have, your code will be much more tidy.

http://www.cplusplus.com/reference/locale/tolower/
http://en.cppreference.com/w/cpp/string/byte/tolower


Use a bool controlled while loop, similar to here, you can use a char instead of int

http://www.cplusplus.com/forum/beginner/99203/#msg534078

I am not a fan of do loops, and I really dislike hate with a passion conditionals like this:

1
2
3
do {

}while (choice != 'q' && choice != 'Q');


They are ugly, error prone, non scalable, and did I say UGLY :+( , and in this case unnecessary.

Good Luck !!

Good idea on the tolower call!

One thing though, the link you provided me to show a menu, wow it is way more complex then a simple do while loop! I don't understand all the hate with a do while loop, the conditions are simple, the execution is easy and the logic is easy to understand!

the example you provided looks like its all over the place
One thing though, the link you provided me to show a menu, wow it is way more complex then a simple do while loop! I don't understand all the hate with a do while loop, the conditions are simple, the execution is easy and the logic is easy to understand!


All 3 of the loop types can be converted from one to another, so maybe at the price of an extra variable we can avoid do loops. do loops can be error prone. The main reason people might choose to use one, is that value of a variable in the condition is not known until some time in the body of the loop. The fact that it always executes at least once is not a reason to prefer a do loop IMO.

It is the ugly condition that I hate, I am not a fan of do loops. Do some situations where a do loop is handy.

Kernighan and Ritchie C Programming wrote:
Experience shows that do-while is much less used than while and for. Nonetheless, from
time to time it is valuable, as in the following function itoa, which converts a number to a
character string (the inverse of atoi).



the example you provided looks like its all over the place


Probably because I adapted the OP's code from that topic, back then. It is a little more complex, but it does what we want: Loops until quit, shows a menu, and has a default: case for bad input. I usually prefer to have each case call it's own function, that is more tidy :+)

There is a direction relation between switch and an if else if else chain, One has to use the latter if the variable is not a compile time constant int or char.
Topic archived. No new replies allowed.