do-while循环问题

时间:2016-09-29 04:50:16

标签: c++ loops 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;
            }
        }
    }
}

4 个答案:

答案 0 :(得分:1)

我希望do-while循环以这种方式工作。可悲的是没有。

这个循环...

while (someCondition) {
 // stuff
}
除了它将始终第一次运行之外,

与另一个相同。

do {
 // stuff
}
while (someCondition);

可悲的是,没有一个循环结构像中那样在中间断裂。这就是break的用途。

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

答案 1 :(得分: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);
但是,这似乎相当苛刻。除1234以外的任何输入,程序都将退出循环。最好告知用户他们提供了错误的输入,以便他们提供可接受的输入。

答案 2 :(得分:0)

在熟悉代码之前,最好分析问题背后的逻辑。说到这个,它包含了这些步骤:

1.print提示信息到屏幕
 2.阅读用户的选择
 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;
    }
}

答案 3 :(得分:-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
相关问题