I am trying to create a menu driven program, with a for loop and nested do while loop, however, the for loop is repeating infinitely and the do while loop is not repeating even when conditions are met. I have tried using if else statements for the subChoices instead of switch, but that did not change anything in the output. I don't seen anything wrong with the logic, so guessing I have messed up the loop somehow else. I want the loop to repeat for each user until they are done by entering 4, and then repeat for other users up to a max of 3.
int main() {
// Variable declarations
string lineOfStars(60, '*');
int numA1 = 0, numA2 = 0, numA3 = 0;
int numD1 = 0, numD2 = 0, numD3 = 0;
int numM1 = 0, numM2 = 0, numM3 = 0, numM4 = 0;
double total = 0;
string name;
int numPeople;
int mainChoice;
int subChoice;
// Print header
cout << endl << lineOfStars << endl;
cout << setw(40) << "HOGWARTS RESTAURANT";
cout << endl << lineOfStars << endl;
// Start user input
cout << "How many people are included in your order? ";
cin >> numPeople;
// Input validation for number of people
while(numPeople < 1 || numPeople > 3) {
cout << "Oops! Please select 1, 2, or 3." << endl;
cout << "How many people are included in your order? ";
cin >> numPeople;
}
// Outer loop for each person
for(int i = 1; i <= numPeople; i++) {
// Start of menu selection loop for each person
do {
cout << "Person " << i << ", what is your name? ";
cin.ignore();
getline(cin, name);
cout << "\nHi, " << name << ". What would you like to order?" << endl;
cout << "1. Appetizer\n2. Drinks\n3. Meal\n4. I'm done!\n";
cout << "Enter 1-4: ";
cin >> mainChoice;
// Input validation for main menu choice
while(mainChoice < 1 || mainChoice > 4) {
cout << "Oops! That isn't a valid choice." << endl;
cout << "Enter 1-4: ";
cin >> mainChoice;
}
// Process user's main menu choice
switch(mainChoice) {
case 1: // Appetizer
cout << "\nGreat, you have selected Appetizer!" << endl;
cout << "Choose from the following appetizers:" << endl;
cout << "1. Dumbledonuts\t$2.99\n2. Potterfries\t$5.99\n3. Griffintoes\t$4.99\n";
cout << "Choose 1-3: ";
cin >> subChoice;
// Input validation for appetizer choice
while(subChoice < 1 || subChoice > 3) {
cout << "Oops! That isn't a valid choice." << endl;
cout << "Choose 1-3: ";
cin >> subChoice;
}
// Process user's appetizer choice and update total
switch(subChoice) {
case 1:
cout << "How many Dumbledonuts do you want? ";
cin >> numA1;
total += (numA1 * 2.99);
break;
case 2:
cout << "How many Potterfries do you want? ";
cin >> numA2;
total += (numA2 * 5.99);
break;
case 3:
cout << "How many Griffintoes do you want? ";
cin >> numA3;
total += (numA3 * 4.99);
break;
}
break;
case 2: // Drinks
cout << "\nGreat, you have selected Drinks!" << endl;
cout << "Choose from the following drinks:" << endl;
cout << "1. Polyjuice Potion\t$5.99\n2. Death Eater Negroni\t$6.99\n3. Butter Beer\t$4.99\n";
cout << "Choose 1-3: ";
cin >> subChoice;
// Input validation for drink choice
while(subChoice < 1 || subChoice > 3) {
cout << "Oops! That isn't a valid choice." << endl;
cout << "Choose 1-3: ";
cin >> subChoice;
}
// Process user's drink choice and update total
switch(subChoice) {
case 1:
cout << "How many Polyjuice Potions do you want? ";
cin >> numD1;
total += (numD1 * 5.99);
break;
case 2:
cout << "How many Death Eater Negronis do you want? ";
cin >> numD2;
total += (numD2 * 6.99);
break;
case 3:
cout << "How many Butter Beers do you want? ";
cin >> numD3;
total += (numD3 * 4.99);
break;
}
break;
case 3: // Meal
cout << "\nGreat, you have selected Meal!" << endl;
cout << "Choose from the following meals:" << endl;
cout << "1. Griffinwings (10pcs)\t$10.99\n2. Voldenose\t$14.99\n3. Hufflemuffin\t$8.99\n4. Ravenclaw Meat\t$19.99\n";
cout << "Choose 1-4: ";
cin >> subChoice;
// Input validation for meal choice
while(subChoice < 1 || subChoice > 4) {
cout << "Oops! That isn't a valid choice." << endl;
cout << "Choose 1-4: ";
cin >> subChoice;
}
// Process user's meal choice and update total
switch(subChoice) {
case 1:
cout << "How many Griffinwings (10pcs) do you want? ";
cin >> numM1;
total += (numM1 * 10.99);
break;
case 2:
cout << "How many Voldenoses do you want? ";
cin >> numM2;
total += (numM2 * 14.99);
break;
case 3:
cout << "How many Hufflemuffins do you want? ";
cin >> numM3;
total += (numM3 * 8.99);
break;
case 4:
cout << "How many Ravenclaw Meats do you want? ";
cin >> numM4;
total += (numM4 * 19.99);
break;
}
break;
}
} while(mainChoice != 4); // End of do-while loop for each person
}
For submenus you had not added the do while loop. for loop terminates correctly. Here is the working code. The program works fine now.