gpt4 book ai didi

C++ 食物菜单(使用 do while 循环)

转载 作者:行者123 更新时间:2023-11-30 05:37:55 25 4
gpt4 key购买 nike

我在运行这个程序时遇到问题:我试图让用户输入他们在菜单上的选择(A、B、C、D 或 E ),如果他们选择是 选项。如果没有,则直接计算总销售价格。但是每当我选择是时,它似乎重复显示菜单并且不显示选项。请帮助我是 c++

的新手
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

double a = 5.99, b = 4.99, c = 4.99, d = 5.99, e = 9.99, totalprice;
const double TAX = 0.13;
int choice = 0;
char (answer);

do
{

cout << "\nGood day! Welcome to The Bakery! What would you like today?\n";
cout << "\nMenu\n Price"<< endl;
cout << "A: Earl Gray Tea and Biscuits - $" << a << endl;
cout << "B: Coffee and a blueberry scone - $" << b << endl;
cout << "C: Espresso and a tea biscuit - $" << c << endl;
cout << "D: Coffee and a Muffin- $" << d << endl;
cout << "E: The Assorted Tea, Scones, and Biscuits Platter- $" << e << endl;

cout << "\nAre there any addtional orders? 'Y' or 'N'\n" << endl;
cin >> answer;
if (answer == 'Y' || answer == 'y')

{ //Display Choice

cout << "\nYour choice?\n" << endl;
}


if (choice == 'A' || choice == 'a')
{

cout << "A: Earl Gray Tea and Biscuits" << a << endl;

}


if (choice == 'B' || choice == 'b')
{

cout << "B: Coffee and a blueberry scone" << b << endl;

}


if (choice == 'C' || choice == 'c')
{

cout << "A: Earl Gray Tea and Biscuits" << c << endl;

}


if (choice == 'D' || choice == 'd')
{

cout << "D: Coffee and a Muffin" << d << endl;

}


if (choice == 'E' || choice == 'e')
{

cout << "E: The Assorted Tea, Scones, and Biscuits Platter" << e << endl;

}

else if (answer == 'N' || answer == 'n')
{
cin >> totalprice;
cout << "The final bill for today is ";
}

else //Displaying error message
{
cout << "Invalid input";
}

} while (answer != 'Y' && answer != 'y');

}

最佳答案

变量choice应该是一个字符,你必须添加cin来接收choice的输入

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

double a = 5.99, b = 4.99, c = 4.99, d = 5.99, e = 9.99, totalprice;
const double TAX = 0.13;
char choice;
char answer;

do
{

cout << "\nGood day! Welcome to The Bakery! What would you like today?\n";
cout << "\nMenu\n Price"<< endl;
cout << "A: Earl Gray Tea and Biscuits - $" << a << endl;
cout << "B: Coffee and a blueberry scone - $" << b << endl;
cout << "C: Espresso and a tea biscuit - $" << c << endl;
cout << "D: Coffee and a Muffin- $" << d << endl;
cout << "E: The Assorted Tea, Scones, and Biscuits Platter- $" << e << endl;
cin >> choice;
cout << "\nAre there any addtional orders? 'Y' or 'N'\n" << endl;
cin >> answer;
if (answer == 'Y' || answer == 'y')

{ //Display Choice

cout << "\nYour choice?\n" << endl;
}


if (choice == 'A' || choice == 'a')
{

cout << "A: Earl Gray Tea and Biscuits" << a << endl;

}


if (choice == 'B' || choice == 'b')
{

cout << "B: Coffee and a blueberry scone" << b << endl;

}


if (choice == 'C' || choice == 'c')
{

cout << "A: Earl Gray Tea and Biscuits" << c << endl;

}


if (choice == 'D' || choice == 'd')
{

cout << "D: Coffee and a Muffin" << d << endl;

}


if (choice == 'E' || choice == 'e')
{

cout << "E: The Assorted Tea, Scones, and Biscuits Platter" << e << endl;

}

else if (answer == 'N' || answer == 'n')
{
cin >> totalprice;
cout << "The final bill for today is ";
}

else //Displaying error message
{
cout << "Invalid input";
}

} while (answer != 'Y' && answer != 'y');

}

关于C++ 食物菜单(使用 do while 循环),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33060626/

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