2013-03-14 25 views
0

我是一名初學者CS學生,他正在嘗試使用父母餐廳的號碼編制「收銀員」系統。C++當輸出矢量列表時,輸出的數量是平方的

到目前爲止,程序只能添加表1中的前兩個開胃菜,然後在屏幕上打印總稅和稅。

這裏我的問題是,雖然程序按照我現在想要的方式運行,但當我嘗試「查看總數」時,輸出的項目被平方......意思是如果我添加項目「KIMBAB」一次,它將僅在輸出的列表中顯示一個;但是,如果我添加兩次KIMBAB,輸出的項目數變爲4,如果我加3,則輸出9等。

如果我還使用「JON」(Appetizer菜單中的項目2)並添加KIMBAB, JON然後KIMBAB再次,輸出看起來像

KIMBAB

KIMBAB

KIMBAB

JON

JON

JON

KIMBAB

KIMBAB

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; 
} 
+1

我在惠普打印機的地方工作過。而不是5個副本,它會打印25. – 2013-03-14 22:57:27

+7

我敢說,我從來沒有見過'numOfDeclaredVariables/linesOfCode'這樣的比例。 – 2013-03-14 23:00:22

+1

您可能希望考慮代碼的體系結構。 – 2013-03-14 23:04:37

回答

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()); 

這幾年的變化將極大地減少代碼的大小,並提高其可讀性,和你發現的缺陷的能力。

3

問題是你的雙循環if(selectInput ==「8」)。這將平方輸出的數量。第二個循環(價格循環)完全沒有使用,應該刪除。即使對於一個線路循環,通過使用圓括號和適當的縮進來預防未來的這個問題。

請,請使用數組,不要使用字符串,如果你實際上不想存儲文本,並避免gotos。

+0

+1爲努力提供答案,併爲陣列的建議!他的代碼*方式*比它需要的更長。 – JBentley 2013-03-14 23:27:11

+0

OP在這裏。我想花時間感謝那些經歷過我在這裏的代碼的絕對陰謀。 作爲一名介紹學生,我一直試圖通過實踐和應用我學到的東西來學習和提高。 我將學習更多的結構和數組。 非常感謝大家! – user2171877 2013-03-15 02:18:59

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名單剛剛創建的菜單項列表,或者你甚至可以創建一個數組像這樣的:

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; 
     } 
    } 
} 

像這樣會使你的代碼更容易閱讀沒有得到太先進了(希望)爲你的水平。當然,有更好的方法可以做到這一點,但這對您而言可能是一個好的開始。