2016-09-29 173 views
0

我很困惑如何在while循環正常工作時做到這一點。目標是讓代碼循環回「主菜單」,並允許您選擇另一個項目並將其添加到您的「購物車」中,因爲缺乏更好的單詞。問題是我的代碼現在只會給你所選的第一個項目的小計,然後退出而不返回到主菜單,我希望能夠循環進入菜單,直到退出被選中,然後計算總數。任何幫助表示讚賞!Do-while循環的問題

int main() 
{ 
    int dice;       //Amount of dice 
    int beads;       //Amount of beads 
    int bobble;       //Amount of bobble heads 
    int selection;      //Selection 

    string code;      //User input coupon code 

    double diceTotal;     //Subtotal for dice 
    double beadsTotal;     //Subtotal for beads 
    double bobbleTotal;     //Subtotal for bobble heads 
    double total;      //Total cost of purchase 

    const double DICE = 6.25;   //Dice cost 
    const double BEADS = 2.25;   //Bead cost 
    const double BEADS_COUPON = 1.50; //Bead cost w/coupon 
    const double BOBBLE1 = 16.99;  //1-5 Bob. Head cost 
    const double BOBBLE2 = 14.99;  //6-10 Bob. Head cost 
    const double BOBBLE3 = 12.99;  //11+ Bob. Head cost 
    const string COUPON = "beads1";  //Coupon code 

    cout << fixed << setprecision(2); 

    //Welcome 
    do 
    { 
     cout << setw(50) << "Welcome to DecoCar!" << endl; 
     cout << setw(49) << "Nick Wester, Owner" << endl; 
     cout << "Our inventory: " << endl; 
     cout << "1. Fuzzy Dice" << endl; 
     cout << "2. Mardi Gras Beads" << endl; 
     cout << "3. Bobble Heads" << endl; 
     cout << "4. Exit" << endl << endl; 
     cout << "Please make a selection: "; 
     cin >> selection; 
    } 
    while (selection <= 0 || selection >= 5); 
    { 
     if (selection == 1) 
     { 
      cout << "How many Fuzzy Dice would you like to buy? "; 
      cin >> dice; 
      while (dice < 0) 
      { 
       cout << "How many Fuzzy Dice would you like to buy? "; 
       cin >> dice; 
      } 
      diceTotal = dice*DICE; 
      cout << "Your subtotal for the Fuzzy Dice: $" << diceTotal << endl; 
     } 
     else if (selection == 2) 
     { 
      cout << "How many sets of Mardi Gras beasd would you like to buy? "; 
      cin >> beads; 
      cout << "Please type in your coupon code or NONE: "; 
      cin >> code; 
      if (code == "beads1") 
      { 
       cout << "Valid code entered" << endl; 
       beadsTotal = beads*BEADS_COUPON; 
       cout << "Your subtotal for Mardi Gras beads: $" << beadsTotal << endl; 
      } 
      else 
      { 
       beadsTotal = beads*BEADS; 
       cout << "Your subtotal for Mardi Gras beads: $" << beadsTotal << endl; 
      } 
     } 
     else if (selection == 3) 
     { 
      cout << "How many Bobble Heads would you like to buy? "; 
      cin >> bobble; 
      if (bobble >= 1 && bobble <= 5) 
      { 
       bobbleTotal = bobble*BOBBLE1; 
       cout << "Your subtotal for Bobble Heads: $" << bobbleTotal << endl; 
      } 
      else if (bobble >= 6 && bobble <= 10) 
      { 
       bobbleTotal = bobble*BOBBLE2; 
       cout << "Your subtotal for Bobble Heads: $" << bobbleTotal << endl; 
      } 
      else if (bobble >= 11) 
      { 
       bobbleTotal = bobble*BOBBLE3; 
       cout << "Your subtotal for Bobble Heads: $" << bobbleTotal << endl; 
      } 
     } 
    } 
} 
+0

你的循環將永遠爲值1-4的出口,這是你的工作代碼或你還沒有運行它? –

+0

@RishabhKumar一切都「有效」,它只是不會循環回菜單,一旦我選擇一個項目,我想要的數量就會給我一個小計然後退出。 – Nick5227

回答

-1

它看起來像你的代碼可能需要進行一些重新設計:

試試這個僞代碼:

do 
{ 
    Ask for selection 

    Switch(selection) 
    { 
    Case 1: ask for quantity and break; 
    Case 2: same here 
    Case 3: same 
    Case 4: same 
    Default : break 
    } 

    Calculate the subtotal based on above case selected. 
}while(selection is greater than 0 and less than 5); 


Finally print the total amount 
+0

我不想擺脫循環。我已經添加了,所以這個nxt case不會被執行 –

+0

都一樣。使用有效的語法。 –

+0

這實際上是最有幫助的。案例陳述允許我循環並清理我的代碼。謝謝! – Nick5227

0

您已將while部分放在錯誤位置後的語句順序。他們需要在while之前。

do 
{ 
    cout << setw(50) << "Welcome to DecoCar!" << endl; 
    cout << setw(49) << "Nick Wester, Owner" << endl; 
    cout << "Our inventory: " << endl; 
    cout << "1. Fuzzy Dice" << endl; 
    cout << "2. Mardi Gras Beads" << endl; 
    cout << "3. Bobble Heads" << endl; 
    cout << "4. Exit" << endl << endl; 
    cout << "Please make a selection: "; 
    cin >> selection; 

    if (selection == 1) 
    { 
     ... 
    } 
    else if (selection == 2) 
    { 
     ... 
    } 
    else if (selection == 3) 
    { 
     ... 
    } 

    // Add this check 
    else if (selection == 4) 
    { 
     break; 
    } 
    else 
    { 
     // Print a message about the unknown selection. 
    } 
} 
while (true); 
// No need for the additional checks. 
// while (selection <= 0 || selection >= 5); 

PS

while (selection <= 0 || selection >= 5); 

應該已經

while (selection > 0 && selection < 5); 

這似乎相當嚴厲,但。除1,2,34以外的任何輸入,程序將退出循環。最好告訴用戶他們提供了錯誤的輸入,以便他們可以提供可接受的輸入。

+0

這仍然沒有導致它爲我循環。但是,我認爲這是正確的方向 – Nick5227

+0

如果您輸入'1'作爲您的輸入,'選擇<= 0 || selection> = 5'評估爲'false'。 –

0

在用代碼弄髒手之前,最好分析問題背後的邏輯。而這一次的說,它包含這些步驟:

1.打印提示信息篩選
2.read在用戶的選擇
3.1選擇的一些具體的數值表示整個過程
3.2其他值來結束一些任務,然後重複步驟1

那麼它是一個簡單的任務寫下代碼:

while(true) {  // -> while/do-while/for has no difference here, what we need is an endless loop 
    cout<<"welcom info"; // step 1. 
    cin>>selection;  // step 2. 
    if (/* selection out of expected range */) 
     break;   // step 3.1 jump out of the loop; 
    switch(selection) { // step 3.2 Both switch-case/ if-else-if are OK 
    case 0: /* bla */ break; 
    case 1: /* yada */ break; 
    } 
} 
+0

儘管此代碼片段可能會解決問題,但[包括解釋](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 –

+0

@ J.Chomel你說得對,我認爲這段代碼非常明顯。我會爲未來的讀者添加一些解釋。 –

1

我希望做-while循環的工作這種方式。可悲的是沒有。

這個循環...

while (someCondition) { 
// stuff 
} 

是一樣的,因爲這另外一個不同之處,它會永遠運行的第一次。

do { 
// stuff 
} 
while (someCondition); 

可悲的是,沒有像這樣在中間斷開的循環結構。這就是break的用途。

while (true) { 
    // read something 
    if (exitCondition) break; 
    // use what you read 
}