gpt4 book ai didi

c++ - 在 C++ 中切换到循环

转载 作者:太空宇宙 更新时间:2023-11-04 13:49:04 24 4
gpt4 key购买 nike

我正在尝试完成我的 C++ 类(class)的作业,但我真的卡住了。我不确定我是否正确地解决了这个问题。我正在尝试制作一个基于菜单的程序,它将根据用户的菜单选择将项目添加到“购物车”,并允许用户增加他们的“购物车”中的门票数量。

然后,如果他们按 N,它将返回到主菜单,只有当他们按 5“结帐”时,它才会显示他们的订单总额。

非常感谢任何帮助。

谢谢

#include <iostream>

using namespace std;

int main()
{
char menuOpt;
int total = 0;
char moreTix;

cout << "Use menu options 1-5 to add a ticket to your cart." << endl;
cout << "Option 1: Adult - Airboat ride & alligator wrestling show.\n";
cout << "\t Price: $10" << endl;
cout << "Option 2: Adult - Airboat ride, alligator wrestling show & photo with wrestler.\n";
cout << "\t Price: $15" << endl;
cout << "Option 3: Senior - Airboat ride & alligator wrestling show.\n";
cout << "\t Price: $7" << endl;
cout << "Option 4: Child - Airboat ride, alligator wrestling show & stuffed toy alligator.\n";
cout << "\t Price: $10" << endl;
cout << "Option 5: Checkout"
cout << "Option: ";
cin >> menuOpt;

do{
switch(menuOpt){
case '1': cout << "Adult airboat ride & alligator wrestling show has been added to your cart." << endl;
total += 10;
break;
case '2': cout << "Adult airboat ride, alligator wrestling show & photo with wrestler has been added to your cart." << endl;
total += 15;
break;
case '3': cout << "Senior airboat ride & alligator wrestling show has been added to your cart." << endl;
total += 7;
break;
case '4': cout << "Child airboat ride, alligator wrestling show & stuffed toy alligator has been added to your cart." << endl;
total +=10;
break;
case '5': cout << "The total for your purchase is $" << total << endl;
default: cout << "You have made an invalid choice. Please make another selection." << endl;
}
cout << "Would you like to purchase another ticket? (Y/N) ";
cin >> moreTix;
}while (moreTix == 'y' || moreTix == 'Y');

return 0;
}

最佳答案

如果我对您的问题的理解正确,一旦用户选择购买更多门票,您将无法看到菜单。

将你的 do 语句放在这一行之上,它应该可以工作:

cout << "Use menu options 1-5 to add a ticket to your cart." << endl;  

关于c++ - 在 C++ 中切换到循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24297872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com