2016-09-29 346 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 
}