Do-while loop

closed account (ozyMoG1T)

Every time I run the program, regardless what I put, it keeps saying "Please enter a valid menu choice".

Can someone please help me with this problem (I am still new to c++).

Thank you.
Last edited on
Hi,

I think it would be a really good idea to do menu selection via a switch statement or an if else if else chain.

Also the logic will be a lot easier if you convert the input to upper case using the toupper function. That saves for having to test for both lower and upper case input.

Statements like this are error prone, non scalable, and UGLY :+)

while (choice != 'a' || choice != 'A' || choice != 'b' while (choice != 'c' && choice != 'C');

I would also not use a do loop, just because this always executes once is not necessarily a reason to use it. All 3 loops can be converted from 1 to another - can you figure out how to use a while loop with a switch inside ? You can make use of a bool quit variable.

Your code would also benefit from having more functions. If you use a switch, each case can call a function. A switch can have a default: case, you can use this to catch bad input.

Hope all goes well, and we look forward to seeing your new code :+)
closed account (ozyMoG1T)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;

int main()
{
    
        string name, Class;
        cout << "Please enter your name" << endl;
        cin >> name;
        
        cout << "What class are you using this program for?" << endl;
        cin >> Class;
        
        cout << name << ", Welcome to you Magic Number program. I hope it helps you with your " << Class << " class." << endl;
 	
	char choice;       
	
	cout << "Please choose an option below:" << endl;
    	cout << "a: Display an example" << endl;
    	cout << "b: Try it out yourself!" << endl;
    	cout << "c: Exit" << endl;
    	cout << "Enter your choice" << endl;
    	cin >> choice;
 
	while (choice != 'c' || choice != 'C')
	{
		switch (choice)
		{
			case 'a':
			case 'A':
				cout << "here is the example:" << endl;
				break;
			case 'b':
			case 'B':
				cout <<" Try it out yourself" << endl;
				break;
			case 'c':
			case 'C': 
				cout << "Goodbye" << endl;
				break;
			default:
				cout << "Please choose a valid menu choice" << endl;
		}
	}
    return 0;
}


I tried to do this, but it still didn't work. Would you please tell me where is my mistake?

Thank you so much.
#include <string>
and
while (choice != 'c' && choice != 'C')
and
you should include the choices and cin in the loop so that the user can choose again
Last edited on
closed account (ozyMoG1T)
Still, when I run the program, and I put a/b/c/A/B/C in. It still didn't work.
closed account (E0p9LyTq)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>

int main()
{

   std::string name;
   std::string Class;

   std::cout << "Please enter your name: ";
   std::cin >> name;

   std::cout << "What class are you using this program for? ";
   std::cin >> Class;

   std::cout << name << ", Welcome to you Magic Number program. I hope it helps you with your " << Class << " class.\n";

   std::cout << "Please choose an option below:\n";
   std::cout << "a: Display an example\n";
   std::cout << "b: Try it out yourself!\n";
   std::cout << "c: Exit\n";

   char choice = '\0';

   while (choice != 'C')
   {
      std::cout << "Enter your choice: ";
      std::cin >> choice;
      choice = toupper(choice);

      switch (choice)
      {
         case 'A':
            std::cout << "here is the example:\n";
            break;

         case 'B':
            std::cout <<" Try it out yourself\n";
            break;

         case 'C':
            std::cout << "Goodbye\n";
            break;

         default:
            std::cout << "Please choose a valid menu choice\n";
      }
   }

   return 0;
}
Topic archived. No new replies allowed.