gpt4 book ai didi

c++ - 使用枚举类型c++类写一个卖爆米花和饮料的程序

转载 作者:搜寻专家 更新时间:2023-10-31 02:16:54 24 4
gpt4 key购买 nike

我的任务是:编写一个使用枚举类型销售爆米花和饮料的程序。您必须在您的程序:枚举大小 {SMALL, MEDIUM, LARGE, JUMBO};大小爆米花大小,饮料大小;你应该创建一个菜单,要求用户选择他们想要的饮料的大小,并选择什么大小的他们想要的爆米花。然后你应该打印出饮料和爆米花的总成本。

价格:爆米花小= 1.25,中= 2.25,大= 3.50,大= 4.25苏打小=1.50,中=2.50,大=3.75,大=4.50

这是我的代码:

int main()
{
enum sizes { SMALL, MEDIUM, LARGE, JUMBO };
sizes popcornSize, drinkSize;

double sp=1.25, mp=2.25, lp=3.50, jp=4.25, TOTALp, TOTALs, ss=1.50, ms=2.50, ls=3.75, js=4.50 ;
char choice, answer, Psize, Ssize;
int how_many;
cout << "Hello I am selling popcorn and sodas, would you like to buy some? type yes or no please." << endl;
cin >> choice;
if (choice == 'yes')
{
cout << "Great, what would you like popcorn or soda? Type P for popcorn and S for soda." << endl;
cin >> answer;
if (answer = 'P')
{
cout << "what size popcorn would you like (type s for small, m for medium, l for large or j for jumbo) and how many (type a single number)?" << endl;
cin >> Psize >> how_many;
if (Psize = 's')
{
TOTALp = sp*how_many;
cout << "Okay you total is: " << TOTALp << endl;
}
else if (Psize = 'm')
{
TOTALp = mp*how_many;
cout << "Okay you total is: " << TOTALp << endl;
}
else if (Psize = 'l')
{
TOTALp = lp*how_many;
cout << "Okay you total is: " << TOTALp << endl;
}
else if (Psize = 'j')
{
TOTALp = jp*how_many;
cout << "Okay you total is: " << TOTALp << endl;
}


else if (answer = 'S')
{

cout << "what size soda would you like (type small, medium, large or jumbo) and how many (type a single number)?" << endl;
cin >> Ssize >> how_many;
if (Ssize = 's')
{
TOTALs = ss*how_many;
cout << "Okay you total is: " << TOTALs << endl;
}
else if (Ssize = 'm')
{
TOTALs = ms*how_many;
cout << "Okay you total is: " << TOTALs << endl;
}
else if (Ssize = 'l')
{
TOTALs = ls*how_many;
cout << "Okay you total is: " << TOTALs << endl;
}
else if (Ssize = 'j')
{
TOTALs = js*how_many;
cout << "Okay you total is: " << TOTALs << endl;
}

}
cout << "Thanks for buying come again soon." << endl;
}

}
else if (choice == 'no')
cout << "Okay have a great day!" << endl;
return 0;
}

我需要帮助在程序中实现枚举,以及如何将所有这些 if/else 语句放入 switch 结构中。此外,出于某种原因,当我在用户输入他们的选择后运行我的程序时,程序结束,我无法弄清楚为什么。

最佳答案

您的程序结束,因为选择永远不会是"is"或“否”。由于 choice 是一个字符,它只能是一个字符。将"is"更改为“y”,将“否”更改为“n”,它应该可以工作。

我将举例说明如何编写 switch 语句,以便您可以根据其余代码调整它。

#include "stdio.h"
#include "iostream"

using namespace std;

int main()
{
enum sizes { SMALL, MEDIUM, LARGE, JUMBO };
sizes popcornSize, drinkSize;

double sp=1.25, mp=2.25, lp=3.50, jp=4.25, TOTALp, TOTALs, ss=1.50, ms=2.50, ls=3.75, js=4.50 ;
char choice, answer, Psize, Ssize;
int how_many;
cout << "Hello I am selling popcorn and sodas, would you like to buy some? type yes or no please." << endl;
cin >> choice;
switch(choice)
{
case 'y':
cout << "Great, what would you like popcorn or soda? Type P for popcorn and S for soda." << endl;
cin >> answer;
if (answer = 'P')
{
cout << "what size popcorn would you like (type s for small, m for medium, l for large or j for jumbo) and how many (type a single number)?" << endl;
cin >> Psize >> how_many;
if (Psize = 's')
{
TOTALp = sp*how_many;
cout << "Okay you total is: " << TOTALp << endl;
}
else if (Psize = 'm')
{
TOTALp = mp*how_many;
cout << "Okay you total is: " << TOTALp << endl;
}
else if (Psize = 'l')
{
TOTALp = lp*how_many;
cout << "Okay you total is: " << TOTALp << endl;
}
else if (Psize = 'j')
{
TOTALp = jp*how_many;
cout << "Okay you total is: " << TOTALp << endl;
}


else if (answer = 'S')
{

cout << "what size soda would you like (type small, medium, large or jumbo) and how many (type a single number)?" << endl;
cin >> Ssize >> how_many;
if (Ssize = 's')
{
TOTALs = ss*how_many;
cout << "Okay you total is: " << TOTALs << endl;
}
else if (Ssize = 'm')
{
TOTALs = ms*how_many;
cout << "Okay you total is: " << TOTALs << endl;
}
else if (Ssize = 'l')
{
TOTALs = ls*how_many;
cout << "Okay you total is: " << TOTALs << endl;
}
else if (Ssize = 'j')
{
TOTALs = js*how_many;
cout << "Okay you total is: " << TOTALs << endl;
}

}
cout << "Thanks for buying come again soon." << endl;
}
break;
case 'n':
cout << "Okay have a great day!" << endl;
break;
default:
cout << "I dont understand...!" << endl;
break;
}
return 0;
}

关于c++ - 使用枚举类型c++类写一个卖爆米花和饮料的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36417459/

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