我正在尝试创建一个菜单驱动程序,带有 for 循环和嵌套的 do while 循环,但是,for 循环无限重复,而 do while 循环即使满足条件也不会重复。我尝试对 subChoices 使用 if else 语句而不是 switch,但这并没有改变输出中的任何内容。我没有发现逻辑有任何问题,所以猜测我以其他方式搞乱了循环。我希望为每个用户重复该循环,直到输入 4 完成为止,然后为其他用户重复最多 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
}
对于子菜单,您没有添加 do while 循环。 for 循环正确终止。 这是工作代码。该程序现在运行良好。
#include <iostream>
using namespace std;
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 << "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++) {
cout << "Person " << i << ", what is your name? ";
cin.ignore();
getline(cin, name);
// Start of menu selection loop for each person
do {
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;
do{
cout << " from the following appetizers:" << endl;
cout << "1. Dumbledonuts\t$2.99\n2. Potterfries\t$5.99\n3. Griffintoes\t$4.99\n4. ExitMenu\n";
cout << "Choose 1-4: ";
cin >> subChoice;
// Input validation for appetizer choice
while(subChoice < 1 || subChoice > 4) {
cout << "Oops! That isn't a valid choice." << endl;
cout << "Choose 1-4: ";
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;
}
}while(subChoice != 4);
break;
case 2: // Drinks
cout << "\nGreat, you have selected Drinks!" << endl;
do{
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\n4. ExitMenu\n";
cout << "Choose 1-4: ";
cin >> subChoice;
// Input validation for drink choice
while(subChoice < 1 || subChoice > 4) {
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;
}
}while(subChoice!=4);
break;
case 3: // Meal
cout << "\nGreat, you have selected Meal!" << endl;
do{
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\n5. ExitMenu\n";
cout << "Choose 1-5: ";
cin >> subChoice;
// Input validation for meal choice
while(subChoice < 1 || subChoice > 5) {
cout << "Oops! That isn't a valid choice." << endl;
cout << "Choose 1-5: ";
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;
}
}while(subChoice !=5);
break;
}
} while(mainChoice != 4); // End of do-while loop for each person
}
}