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

时间:2016-04-05 04:59:17

标签: c++

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

价格: 爆米花小= 1.25,中等= 2.25,大= 3.50,巨型= 4.25 Soda Small = 1.50,medium = 2.50,large = 3.75,jumbo = 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结构。此外,出于某种原因,当我在用户输入他们的选择后运行我的程序时程序结束,我无法找出原因。

1 个答案:

答案 0 :(得分:2)

你的计划结束了,因为选择永远不是'是',也不是'不'。由于选择是一个字符,它只能是一个字符。将'yes'改为'y',将'no'改为'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;
}