c++食物菜单(使用do while循环)
C++ Food Menu (using do while loop )
我在运行这个程序时遇到了麻烦:我试图让用户在菜单上选择input
(A,B,C,D或E),如果他们选择是option
。如果没有,则直接计算总销售价格。但每当我选择"是"时,它似乎都会重复显示菜单,而不显示选项。请帮忙,我对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 << "nMenun 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');
}
变量选择应该是一个字符,您必须添加cin来接收选择
的输入。#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 << "nMenun 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');
}
在你忘记选择的时候。这样做会更简单。
#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 answer;
char choice;
然后输出您的菜单并询问是否还有其他订单。然后输入答案
do {
if(answer == 'Y' || 'y') {
cout << "Your choice?" << endl;
cin << choice
if(choice == 'A' || choice == 'a') {
cout << "A: Earl Gray Tea and Biscuits" << a << endl;
totalprice = totalprice + a;
} else if(choice == 'B' || choice == 'b') {
cout << "B: Coffee and a blueberry scone" << b << endl;
totalprice = totalprice + b;
} else if(choice == 'C' || choice == 'c') {
cout << "C: Espresso and a tea biscuit" << c << endl;
totalprice = totalprice + c;
} else if(choice == 'D' || choice == 'd') {
cout << "D: Coffee and a Muffin " << d << endl;
totalprice = totalprice + d;
} else if(choice == 'E' || choice == 'e') {
cout << "E: The Assorted Tea, Scones, and Biscuits Platter " << e << endl;
totalprice = totalprice + e;
}
cout<< "Are there any additional orders? Y or N << endl;
cin << answer;
} while(answer != 'N' || answer !='n');
totalprice = totalprice * tax;
cout << "The final bill for today is $" << totalprice << endl;
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 即使没有满足他们的条件,我也无法通过一些 do-while 循环
- 如何使用 do while 循环确定最高值和最低值
- do while 循环中的 if 语句以 yes 或 no 结尾
- 使用 while 或 do while 退出循环
- 带有开关语句的 do-while 循环 -- 无穷循环错误
- 为什么当我输入一个被接受的数字时,我的 do-while 循环没有中断?
- 为什么我的 Do-while 循环无法在C++中运行菜单
- 如果语句不会在 do - while 循环中运行
- 如何在c++中读取do-while循环中的空白
- 什么是'do while loop'的真实应用?
- 将嵌套的 for 循环更改为 do-while 循环
- 如何修复不会在退出选择时终止的 do-while 循环
- 为什么这个do-while循环每隔一个循环只写入数组?
- C++ do/while 循环和 if/else: 骰子游戏 -- 第 75 行错误:"Cout"之前的预期"While" -- 如何解决这个问题?
- 卡在链表中的无限 Do-while 循环中
- for(;;) vs do..while() for main program loop
- 试图调节我的 do-while 以停止在终端中退出程序.菜单不会根据需要循环使用当前语句
- 使用基本 do-while 循环求解泊松方程