C++ 输出 vector 列表时,输出量平方

标签 c++ system

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

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

我的问题是,虽然程序现在按我想要的方式运行,但当我尝试“VIEW TOTAL”时,输出的项目是平方的...意味着如果我添加项目“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;
}

最佳答案

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

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

关于C++ 输出 vector 列表时,输出量平方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15421763/

相关文章:

c++ - g++ 编译器无法识别我的函数

c++ - 如何有效地使用 boost::process::async_pipe 进行写入和读取?

linux - 允许 Apache 运行的 Perl 脚本使用 system()

c++ - 在 Linux 中测量 C++ 系统调用命令的时间

system - 分布式系统设计

c++ - 用类别表示有根树

c++ - 更新 ubuntu 10.4 后无法将 errno.h 与 gcc/g++ 一起使用

c++ - 最烦人的解析 : why doesn't `g( ( f() ) );` call `f` 's default constructor and pass the result to `g` 's ctor that takes a `f` ?

system - 可扩展 VS 可扩展

C - 系统调用 - 读/写结构中的错误