0

我是一名 CS 初学者,正在尝试使用我父母餐厅的号码来编写“收银员”系统。

到目前为止,程序只能为表 1 添加前两个开胃菜,然后在屏幕上打印含税的总数。

我的问题是,虽然程序现在以我想要的方式运行,但当我尝试“查看总计”时,输出的项目是平方的......这意味着如果我添加项目“KIMBAB”一次,它只会在输出列表;但是,如果我添加两次 KIMBAB,则输出的项目数变为 4,如果我添加 3,则输出 9 等等。

如果我也使用“JON”(开胃菜菜单上的第 2 项),然后再次添加 KIMBAB、JON 然后 KIMBAB,输出看起来像

金巴

金巴

金巴

乔恩

乔恩

乔恩

金巴

金巴

金巴

以下是我到目前为止所拥有的源代码,如果有人可以帮助我,那就太好了!谢谢!

#include <iostream>
#include <string>
#include <iomanip>
#include <vector>


using namespace std;

float sum(const vector<float>& x);

// prices
        // appetizers
float kimBab=5.50, jon=7.50, kJon=6.50, sTofu=5.50, wanja=7.50, kgSalad=5.50, edamame=4.00, fVeggies=5.50;
        // mains
float BBB=14.50, BGG=17.50, seaFood=19.50, dakChim=17.50, djGalbi=17.50, soGalbi=19.50;
        // soups
float kimchiC=12.50, dwenjangC=12.50, ygJang=14.50, sdbC=12.50, bjC=14.50;
        // rice and noodles
float japChae=14.50, BBK=10.50, ksBokum=12.50, dopBab=12.50;
        // fusion
float cbggQ=13.50, bggB=13.50, bggBag=13.50, bggW=12.50, bggP=12.50;
        // desserts
float breadP=4.00, vIC=3.00, sjG=3.00, gelato=3.50, butterT=2.50;

// item entry
        // appetizers
string kimBabS="KIMBAB", jonS="JON", kJonS="KIMCHI JON", sTofuS="SEASONED TOFU", wanjaS="WANJA", kgSaladS="KOREAN GREEN SALAD", edamameS="EDAMAME", fVeggiesS="FRESH VEGGIES WITH DIP";
        // mains
string BBBS="BIBIMBAB", BGGS="BULGOGI", seaFoodS="SEAFOOD", dakChimS="DAK-CHIM", djGalbiS="DWEJIGALBI-CHIM", soGalbiS="SOGALBI-DUKBEGI";
        // soups
string kimchiCS="KIMCHI-CHIGYE", dwenjangCS="DWENJANG-CHIGYE", ygJangS="YOUKGYEJANG", sdbCS="SOONDUBU-CHIGYE", bjCS="BIJI-CHIGYE";
        // rice and noodles
string japChaeS="JAPCHAE", BBKS="BIBIMKUKSU", ksBokumS="KUKSUBOKUM", dopBabS="DOPBAB";
        // fusion
string cbggQS="CHICKEN BULGOGI QUESADILLA", bggBS="BULGOGI BURGER", bggBagS="BULGOGI BAGUETTE", bggWS="BULGOGI WRAP", bggPS="BULGOGI PANINI";
        // desserts
string breadPS="BREAD PUDDING", vICS="VANILLA ICE CREAM", sjGS="SUJUNGGWA", gelatoS="GELATO", butterTS="BUTTER TART";
        // attributes
string beef="BEEF", pork="PORK", chicken="CHICKEN", shrimp="SHRIMP", tofu="TOFU";

// variables
string table1="1", table2="2", table3="3", table4="4", table5="5", table6="6", table7="7", table8="8", table9="9", table10="10";
string appySelect="1", mainSelect="2", soupSelect="3", riceNoodleSelect="4", dessertSelect="5";
string appyInput, mainInput, soupInput, riceNoodleInput, fusionInput, dessertInput, attInput, addAnotherInput;
string itemEntry1, itemEntry2, itemEntry3, itemEntry4, itemEntry5, itemEntry6, itemEntry7, itemEntry8, itemEntry9, itemEntry10, itemEntry11, itemEntry12, itemEntry13, itemEntry14, itemEntry15, itemEntry16, itemEntry17, itemEntry18, itemEntry19, itemEntry20;
float itemPrice1, itemPrice2, itemPrice3, itemPrice4, itemPrice5, itemPrice6, itemPrice7, itemPrice8, itemPrice9, itemPrice10, itemPrice11, itemPrice12, itemPrice13, itemPrice14, itemPrice15, itemPrice16, itemPrice17, itemPrice18, itemPrice19, itemPrice20;
float totalpreTax, totalpostTax, addHST, addtaxHST=0.14, totaltaxHST=1.14;
// booleans
bool itemOne, itemTwo, itemThree, itemFour, itemFive, itemSix, itemSeven, itemEight, itemNine, itemTen, itemEleven, itemTwelve, itemThirteen, itemFourteen, itemFifteen, itemSixteen, itemSeventeen, itemEighteen, itemNineteen, itemTwenty;
// input variables
string tableNumber;
string selectInput;
string itemInput1, itemInput2, itemInput3, itemInput4, itemInput5, itemInput6, itemInput7, itemInput8, itemInput9, itemInput10, itemInput11, itemInput12, itemInput13, itemInput14, itemInput15, itemInput16, itemInput17, itemInput18, itemInput19, itemInput20;
string itemAtt1, itemAtt2, itemAtt3, itemAtt4, itemAtt5, itemAtt6, itemAtt7, itemAtt8, itemAtt9, itemAtt10, itemAtt11, itemAtt12, itemAtt13, itemAtt14, itemAtt15, itemAtt16, itemAtt17, itemAtt18, itemAtt19, itemAtt20;

// MATH FUNCTIONS
void totalposttaxCalc ()
{
    totalpreTax = itemPrice1 + itemPrice2 + itemPrice3 + itemPrice4 + itemPrice5 + itemPrice6 + itemPrice7 + itemPrice8 + itemPrice9 + itemPrice10 + itemPrice11 + itemPrice12 + itemPrice13 + itemPrice14 + itemPrice15 + itemPrice16 + itemPrice17 + itemPrice18 + itemPrice19 + itemPrice20;
    addHST = totalpreTax * addtaxHST;
    totalpostTax = totalpreTax * totaltaxHST;
    cout << string(50, '\n');
    cout << "Total:  " << "$" << totalpreTax << endl;
    cout << endl;
    cout << "Tax  :  " << "$" << addHST << endl;
    cout << endl;
    cout << endl;

    cout << "Grand Total:  " << "$" << totalpostTax;
    std::cin.get();
}
// SELECTION FUNCTIONS
void tableSelection ()
{
    do
    {
        cout << "Enter table number: ";
        cin >> tableNumber;
    } while ((tableNumber!=table1)&&(tableNumber!=table2)&&(tableNumber!=table3)&&(tableNumber!=table4)&&(tableNumber!=table5)&&(tableNumber!=table6)&&(tableNumber!=table7)&&(tableNumber!=table8)&&(tableNumber!=table9)&&(tableNumber!=table10));
}
void menuSelection ()
{
    do
    {
        cout << "-= MENU =-" << endl;
        cout << "1. Appetizers" << endl;
        cout << "2. Mains" << endl;
        cout << "3. Soups" << endl;
        cout << "4. Rice and Noodles" << endl;
        cout << "5. Fusion" << endl;
        cout << "6. Desserts" << endl;
        cout << "7. Back" << endl;
        cout << endl;
        cout << "8. SEE COMPLETE ORDER." << endl;
        cout << endl;
        cout << "Please enter your selection: ";
        cin >> selectInput;
    } while ((selectInput!="1")&&(selectInput!="2")&&(selectInput!="3")&&(selectInput!="4")&&(selectInput!="5")&&(selectInput!="6")&&(selectInput!="7")&&(selectInput!="8"));
}
void appySelection ()
{
    do
    {
        cout << "-= APPETIZERS =- " << endl;
        cout << "1. " << kimBabS << endl;
        cout << "2. " << jonS << endl;
        cout << "3. " << kJonS << endl;
        cout << "4. " << sTofuS << endl;
        cout << "5. " << wanjaS << endl;
        cout << "6. " << kgSaladS << endl;
        cout << "7. " << edamameS << endl;
        cout << "8. " << fVeggiesS << endl;
        cout << "9. Back" << endl;
        cout << endl;
        cout << "Enter your selection: ";
        cin >> appyInput;
        } while ((appyInput!="1")&&(appyInput!="2")&&(appyInput!="3")&&(appyInput!="4")&&(appyInput!="5")&&(appyInput!="6")&&(appyInput!="7")&&(appyInput!="8")&&(appyInput!="9"));
}
void mainSelection ()
{
    do
    {
        cout << "-= MAINS =-" << endl;
        cout << "1. " << BBBS << endl;
        cout << "2. " << BGGS << endl;
        cout << "3. " << seaFoodS << endl;
        cout << "4. " << dakChimS << endl;
        cout << "5. " << djGalbiS << endl;
        cout << "6. " << soGalbiS << endl;
        cout << "7. Back" << endl;
        cout << endl;
        cout << "Enter your selection: ";
        cin >> mainSelect;
    } while ((mainSelect!="1")&&(mainSelect!="2")&&(mainSelect!="3")&&(mainSelect!="4")&&(mainSelect!="5")&&(mainSelect!="6")&&(mainSelect!="7"));
}
void soupSelection ()
{
    do
    {
        cout << "-= SOUPS =- " << endl;
        cout << "1. " << kimchiCS << endl;
        cout << "2. " << dwenjangCS << endl;
        cout << "3. " << ygJangS << endl;
        cout << "4. " << sdbCS << endl;
        cout << "5. " << bjCS << endl;
        cout << "6. Back" << endl;
        cout << endl;
        cout << "Enter your selection: ";
        cin >> soupInput;
    } while ((soupInput!="1")&&(soupInput!="2")&&(soupInput!="3")&&(soupInput!="4")&&(soupInput!="5")&&(soupInput!="6"));
}
void riceNoodleSelection ()
{
    do
    {
        cout << "-= RICE AND NOODLES =- " << endl;
        cout << "1. " << japChaeS << endl;
        cout << "2. " << BBKS << endl;
        cout << "3. " << ksBokumS << endl;
        cout << "4. " << dopBabS << endl;
        cout << "5. Back" << endl;
        cout << endl;
        cout << "Enter your selection: ";
        cin >> riceNoodleInput;
    } while ((riceNoodleInput!="1")&&(riceNoodleInput!="2")&&(riceNoodleInput!="3")&&(riceNoodleInput!="4")&&(riceNoodleInput!="5"));
}
void fusionSelection ()
{
    do
    {
        cout << "-= FUSION =- " << endl;
        cout << "1. " << cbggQS << endl;
        cout << "2. " << bggBS << endl;
        cout << "3. " << bggBagS << endl;
        cout << "4. " << bggWS << endl;
        cout << "5. " << bggPS << endl;
        cout << "6. Back" << endl;
        cout << endl;
        cout << "Enter your selection: ";
        cin >> fusionInput;
        } while ((fusionInput!="1")&&(fusionInput!="2")&&(fusionInput!="3")&&(fusionInput!="4")&&(fusionInput!="5")&&(fusionInput!="6"));
}
void dessertSelection ()
{
    do
    {
        cout << "-= DESSERTS =- " << endl;
        cout << "1. " << breadPS << endl;
        cout << "2. " << vICS << endl;
        cout << "3. " << sjGS << endl;
        cout << "4. " << gelatoS << endl;
        cout << "5. " << butterTS << endl;
        cout << "6. Back" << endl;
        cout << endl;
        cout << "Enter your selection: ";
        cin >> dessertInput;
        } while ((dessertInput!="1")&&(dessertInput!="2")&&(dessertInput!="3")&&(dessertInput!="4")&&(dessertInput!="5")&&(dessertInput!="6"));
}
void attSelection ()
{
    do
    {
        cout << "-= WHAT KIND =-" << endl;
        cout << "1. " << beef << endl;
        cout << "2. " << pork << endl;
        cout << "3. " << chicken << endl;
        cout << "4. " << shrimp << endl;
        cout << "5. " << tofu << endl;
        cout << endl;
        cout << "Enter your selection: ";
        cin >> attInput;
    } while ((attInput!="1")&&(attInput!="2")&&(attInput!="3")&&(attInput!="4")&&(attInput!="5"));
}
void addAnother ()
{
    do
    {
        cout << endl;
        cout << "Add another item from the same category? (1 for YES, 2 for NO): ";
        cin >> addAnotherInput;
    } while ((addAnotherInput!="1")&&(addAnotherInput!="2"));
}

int main ()
{
    cout << setiosflags(ios::fixed) << setiosflags(ios::showpoint) << setprecision(2);
    mainLoop:
    tableSelection ();
    // IF TABLE 1 START
    if (tableNumber==table1)
    {
        vector<string> tableOneList;
        vector<float> tableOnePriceList;

        tableOneLoop:
        cout << string(50, '\n');
        cout << "-= TABLE 1 =-" << endl;
        cout << endl;
        menuSelection ();
        // IF VIEW TOTAL START
        if (selectInput=="8")
        {
            cout << endl;
            for(vector<string>::const_iterator items = tableOneList.begin(); items != tableOneList.end(); ++items )
            for(vector<float>::const_iterator prices = tableOnePriceList.begin(); prices!= tableOnePriceList.end(); ++prices)
            cout << *items << endl;
            cout << "__________________________________________" << endl;
            cout << "TOTAL: " << "$" << sum(tableOnePriceList) << endl;
            totalpreTax=sum(tableOnePriceList);
            cout << "HST:   " << "$" << totalpreTax*addtaxHST << endl;
            cout << endl;
            cout << "GRAND TOTAL: " << "$" << totalpreTax*totaltaxHST << endl;
        }
        // IF VIEW TOTAL END

        // IF APPY START
        if (selectInput=="1")
        {
            appyLoop:
            appySelection ();
            if (appyInput=="1")
            {
                tableOneList.push_back(kimBabS);
                tableOnePriceList.push_back(kimBab);
                cout << endl;
                cout << kimBabS << " successfully added.";
                cout << endl;
                goto tableOneLoop;
            }
            if (appyInput=="2")
            {
                tableOneList.push_back(jonS);
                tableOnePriceList.push_back(jon);
                cout << endl;
                cout << jonS << " successfully added.";
                cout << endl;
                addAnother ();
                if (addAnotherInput=="1")
                {
                    goto appyLoop;
                }
                if (addAnotherInput=="2")
                {
                    goto tableOneLoop;
                }
            }
            if (appyInput=="3")
            {
                tableOneList.push_back(kJonS);
                tableOnePriceList.push_back(kJon);
            }
            if (appyInput=="4")
            {
                tableOneList.push_back(sTofuS);
                tableOnePriceList.push_back(sTofu);
            }
            if (appyInput=="5")
            {
                tableOneList.push_back(wanjaS);
                tableOnePriceList.push_back(wanja);
            }
            if (appyInput=="6")
            {
                tableOneList.push_back(kgSaladS);
                tableOnePriceList.push_back(kgSalad);
            }
            if (appyInput=="7")
            {
                tableOneList.push_back(edamameS);
                tableOnePriceList.push_back(edamame);
            }
            if (appyInput=="8")
            {
                tableOneList.push_back(kimBabS);
                tableOnePriceList.push_back(kimBab);
            }
        }
        // IF APPY END

        // IF MAIN START
        if (selectInput=="2")
        {
            mainSelection ();
        }
        // IF MAIN END

        // IF SOUP START
        if (selectInput=="3")
        {
            soupSelection ();
        }
        // IF SOUP END

        // IF RICENOODLE START
        if (selectInput=="4")
        {
            riceNoodleSelection ();
        }
        // IF RICENOODLE END

        // IF FUSION START
        if (selectInput=="5")
        {
            fusionSelection ();
        }
        // IF FUSION END

        // IF DESSERT START
        if (selectInput=="6")
        {
            dessertSelection ();
        }
        // IF DESSERT END

        // IF BACK START
        if (selectInput=="7")
        {
            goto mainLoop;
        }
        // IF BACK END
    }
}    // IF TABLE 1 END


float sum(const vector<float>& x) {
    float total = 0.0;  // the sum is accumulated here
    for (int i=0; i<x.size(); i++) {
        total = total + x[i];  
    }
    return total;
}
4

3 回答 3

3

问题是 if(selectInput == "8") 下的双 for 循环。这使输出的数量平方。第二个 for 循环(价格循环)完全未使用,应该删除。通过使用括号和适当的缩进来防止将来出现这个问题,即使是单行循环也是如此。

请,请使用数组,如果您实际上不想存储文本,请不要使用字符串,并避免使用 goto。

于 2013-03-14T23:18:32.847 回答
2

我发现很难以目前的形式解决这个问题。

我会通过摆脱重复来开始改变它。

你有很多变量,其中名称、内容、价格是分开保存的。选择它们的编号等。

你需要介绍几个关键的数据结构。我不会为你做所有的事情。

struct Price {
    const unsigned int pennies;
    Price(int pennies);
};

struct Dish {
    const string name;
    const Price price;
    Dish(string name, Price price);
};

struct SubMenu {
    const string name;
    const vector<Dish> dishes;
    SubMenu(string name, vector<Dish> dishes);
};

typedef vector<SubMenu> Menu;

Menu menu;
menu.push_back(
    SubMenu("appetizers", {
        Dish("KIMBAB", Price(550)),
        Dish("JON", Price(750))
        })); // etc

然后你可以说,例如:

SubMenu& subMenu = menu[0];

cout << subMenu.name << endl;
for(int i = 0; i < subMenu.dishes.size(); ++i) {
    cout << i << " " << subMenu.dishes[i] << endl;
}

您应该能够拥有一项功能来打印任何子菜单。

此外,将选项读入一个整数并检查范围:

do {
    unsigned int choice;
    cin >> choice;
} while(choice >= subMenu.dishes.size());

这些很少的更改将大大减少代码的大小,并提高其可读性和查找错误的能力。

于 2013-03-14T23:41:33.850 回答
1

作为一名 CS 初学者,我建议您尝试以下方法来开始您的数据:

struct menuItem
{
    menuItem(string name, MenuTypes type, float price)
    { 
        this.name=name;
        this.type=type; 
        this.price=price;
    }
    string name;
    MenuTypes type;
    float price;
};

enum MenuTypes
{
    Appetizer,
    Main,
    Soup,
    Rice_And_Noodles,
    Fusion,
    Dessert
}

然后你可以使用 STL List 创建一个菜单项列表,或者你甚至可以创建一个这样的数组:

menuItem myMenu[20]; //or whatever # of items you expect in your menu
int curItem = 0;
myMenu[curItem++] = new menuItem("firstItemName", MenuTypes.Appetizer, 1.34f);
myMenu[curItem++] = new menuItem("secondItemName", MenuTypes.Appetizer, 2.35f);
...

只要确保你创建一个足够大的数组顺便说一句,如果你这样做......这肯定会让事情变得更容易......这样你就可以创建一个像这样的菜单:

int displayMenu(MenuTypes theType){
    int curMenuCount = 0;
    for(int i = 0; i < myMenu.size(); i++){
         if(myMenu[i].type == theType){
              cout << ++curMenuCount << myMenu[i].name << endl;
         }
    }
    return curMenuCount;
}

然后找到该项目:

float getPriceForItem(int selection, MenuType theType){
    int curNumForThisMenu = 0;
    for (int i = 0; i < myMenu.size(); i++){
        if(myMenu[i].type == theType) curNumForThisMenu++;
        if(curNumForThisMenu == selection){
            return myMenu[i].price;
        }
    }
}

像这样的东西会让你的代码更容易阅读,而不会让你的水平变得太高级(希望如此)。当然有更好的方法来做到这一点,但这对你来说可能是一个好的开始。

于 2013-03-14T23:37:04.017 回答