Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 简单程序-菜单仅显示一次_C++_Loops_Switch Statement_Logic_Cumulative Sum - Fatal编程技术网

C++ 简单程序-菜单仅显示一次

C++ 简单程序-菜单仅显示一次,c++,loops,switch-statement,logic,cumulative-sum,C++,Loops,Switch Statement,Logic,Cumulative Sum,我正在努力记录购买的食品杂货的总量 在我的程序中,每次我买苹果、奶酪或面包时,程序都会继续显示菜单 但在程序计算出苹果总数后,它会不断询问“有多少个苹果?”而不是返回菜单选择其他项目 也许这与我使用的循环类型有关 我一直在努力想办法解决这个问题。任何帮助都将不胜感激 #include <iostream> #include <iomanip> #include <fstream> using namespace std; int main() {

我正在努力记录购买的食品杂货的总量

在我的程序中,每次我买苹果、奶酪或面包时,程序都会继续显示菜单

但在程序计算出苹果总数后,它会不断询问“有多少个苹果?”而不是返回菜单选择其他项目

也许这与我使用的循环类型有关

我一直在努力想办法解决这个问题。任何帮助都将不胜感激

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
     double BUDGET;
     const double apple= .60;
     const double lb_cheese= 1.60;
     const double loaf_bread = 2.50;

     double total;
     int count;
     char choice;
     double amount_left;

    cout <<"Welcome! What is the budget for your picnic lunch?"<< endl;
    cin>> BUDGET;

    cout<<"Choose one of the following"<<endl;
    cout<<"-------------------------------------"<<endl;
    cout<<"            MENU             \n "<<endl;
    cout<<"A-Apple      B-Cheese        C-Bread"<<endl;
    cout<<" $0.60       $1.50             $2.50       "<<endl;
    cout<<"-------------------------------------"<<endl;
    cin>> choice;



   while ((choice != 'Q') && (total <BUDGET)) //Q is the sentinel value to "quit" the  program
 {


    switch(choice)

  {

    case 'A':
    case 'a':

    cout<<"How many apples?";
    cin>> count;
    total+= (count *apple);
    break;



    case 'B':
    case 'b':

    cout<<"How many pounds of cheese ?";
    cin>> count;

    total+= (count* lb_cheese);
    break;



    case 'C':
    case 'c':
    cout<<"How many loafs of bread?";
    cin>> count;
    total+= (count * loaf_bread);
    break;



    default:
    cout<<"The entry you have entered is not valid, please try again."<<endl;
  }




if( total > BUDGET)
    { cout<<"You have exceeded your budget please check your cart.\n\n";
       break;
       }



cout<<"Your total is: $"<<setprecision((2))<<fixed<<total<<endl;
amount_left= BUDGET-total;
cout<<"You have $"<<setprecision(2)<<fixed<<amount_left<<" left to spend."<<endl;



 }

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
双重预算;
常数双苹果=0.60;
常量双磅奶酪=1.60;
常量双面包=2.50;
双倍总数;
整数计数;
字符选择;
剩下的两倍数量;

cout显示菜单不在循环中:

display menu
read option
while (option != quit) {
    do some calculations
}
因此,菜单仅显示一次。您可以将其更改为无限循环:


另外,尽量避免编写超过50行的函数,将一些逻辑放在不同的函数中,只需调用此函数,将其分解为更小的部分,它将更易于阅读,也更易于理解。

是的,将菜单放入一个循环中,以显示所需的次数,同时请记住将变量初始化为良好做法

双倍总计=0.00//初始化

while (true) {
    display menu
    read option

    if (choice == 'Q' || total >= BUDGET)
        break;

    do some calculations
}