0

私は CS の初心者で、実家のレストランの番号を使って「レジ」システムをプログラムしようとしています。

これまでのところ、プログラムは表 1 の最初の 2 つの前菜のみを追加し、税込みの合計を画面に出力することしかできません。

ここでの私の問題は、プログラムが今のところ思いどおりに実行されている間、「VIEW TOTAL」を実行しようとすると、出力されるアイテムが二乗されるということです...つまり、アイテム「KIMBAB」を一度追加すると、出力されたリスト; ただし、KIMBAB を 2 回追加すると、出力されるアイテムの数は 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") の下の double for ループです。これは、出力数を 2 乗します。2 番目の for ループ (価格ループ) はまったく使用されていないため、削除する必要があります。1 行のループであっても、括弧と適切なインデントを使用して、将来この問題を回避してください。

そして、配列を使用し、実際にテキストを保存したくない場合は文字列を使用せず、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;
}

任意のサブメニューを印刷する 1 つの関数を持つことができる必要があります。

さらに、選択を整数に読み込み、範囲を確認します。

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 に答える