Switch Statment Input Check Help Please

Hi I'm a programming student working on this sample restaurant menu.
The problem im having is when the user enters invalid input the program is supposed to re-ask the question. Closest I've gotten is the FIRST and LAST option of each switch work semi correctly ( I would like it if it only asked the problem question but it restarts the entire loop ) but the MIDDLE cases just roll on through here is a snip of what works and what doesnt:
(I have tried adding 'break;' to the 'default:' case but no affect)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
	cout << "Please enter the number of people in your party. \n";
	cin >> party;
	while (count <= party)
	{//Start Count
	cout << endl << "May I ask who I'm serving today? ";
	cin >> name;
	cout << endl << "Hello " << name <<", May I take your order? \n
         cout << "Enter (A) for Pizza | (B) for Pasta | (C) for Salad. \n";
	cin >> meal;  // Invalid input here takes you to the right place.
	switch (meal) 
	{//Start Meal Switch
	case 'A':
	case 'a':
		{//Start Pizza Switch
		cout << endl << "You chose Pizza! \nSo " << name <<" what would you like for your topping? \n
                  cout << Toppings - Please enter (A) for Pepperoni | (B) for Anchovies | (C) for Plain \n";
		cin >> topping;  //Invalid input here is where help is needed
		switch (topping)
		{//Start Topping Switch
		case 'A':
		case 'a':
			total = total + 1.00;
			cout << endl << "You chose Pepperoni! \n";
			break;
		case 'B':
		case 'b':
			total = total + 2.00;
			cout << endl << "You chose Anchovies! \n";
			break;
		case 'C':
		case 'c':
			cout << endl << "You chose Plain Cheese! \n";
			break;
		default:
			cout << endl << "Invalid Choice. \nPlease start over. \n";
		}//End Topping Switch
		cout << "Now " << name <<" the Size! \n";
		cout << "Size - Please enter (A) for Small | (B) for Medium | (C) for Large \n";
		cin >> size;//Invalid input here takes you to the right place
		switch (size)
		{//Start Size Switch
		case 'A':
		case 'a':
			total = total + 8.00;
			cout << endl << "You chose Small! \nIs this correct? \nPlease enter: \nY for Yes \nN for No";
			cin >> order;
			if (order == 'y')
			{
			count++;
			}
			break;
		case 'B':
		case 'b':
			total = total + 12.00;
			cout << endl << "You chose Medium! \nIs this correct? \nPlease enter: \nY for Yes \nN for No";
			cin >> order;
			if (order == 'y')
			{
			count++;
			}
			break;
		case 'C':
		case 'c':
			total = total + 15.00;
			cout << endl << "You chose Large! \nIs this correct? \nPlease enter: \nY for Yes \nN for No";
			cin >> order;
			if (order == 'y')
			{
			count++;
			}
			break;
		default:
			cout << endl << "Invalid Choice. \nPlease start over. \n";
			break;
		}//End Size Switch
		break;
		}//End Pizza Switch  
Wow, and I didnt even think this would take 30 seconds to answer.
I recommend making functions for each process of your switches.

e.g.
1
2
3
4
5
6
// or whatever you want to call them...
bool orderMeal(const char);
bool orderPizza(const char));
bool orderToppings(const char));
bool orderSize(const char));
//etc... 


You need a loop for each input that you want repeated.
1
2
3
4
5
do
{
//...
cin >> meal;
}while(!orderMeal(meal)}


Inside orderMeal:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
orderMeal(const char choice)
{
    switch(choice)
   {
       case 'A':
               cout << "You chose Pizza!" << endl;
               do
               {
                   cout << "Please choose topping ";
                   cin >> topping;
               }while(!orderTopping))
               return true;
       case 'B':
               //.... etc

        default:
              return false;  //Invalid entry, tells the loop to continue
   }
}


Repeat this for all your conditions or just keep what you have an insert the correct looping into the repeatable choices.
Last edited on
Thank you so much (Functions are litteraly the lecture for tonight) so im sure we will be modifing the same code for functions.

Because the first loop i added worked for half the switches I guess I tuned out the use of more.. wont happen again :)
Topic archived. No new replies allowed.