C ++ Food Menu(使用do while循环)

时间:2015-10-11 01:15:20

标签: c++ if-statement do-while

我在运行此程序时遇到问题:我尝试让用户input在菜单上选择( A,B,C,D或E ),如果他们选择是option。如果没有,那么它直接计算总销售价格。但每当我选择是,它似乎重复显示菜单,并没有显示选择。请帮助我对c++

相当新
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

    double a = 5.99, b = 4.99, c = 4.99, d = 5.99, e = 9.99, totalprice;
    const double TAX = 0.13;
    int choice = 0;
    char (answer);

    do
    {

        cout << "\nGood day! Welcome to The Bakery! What would you like today?\n";
        cout << "\nMenu\n                                                        Price"<< endl;
        cout << "A: Earl Gray Tea and Biscuits  -                        $" << a << endl;
        cout << "B: Coffee and a blueberry scone -                       $" << b << endl;
        cout << "C: Espresso and a tea biscuit -                         $" << c << endl;
        cout << "D: Coffee and a Muffin-                                 $" << d << endl;
        cout << "E: The Assorted Tea, Scones, and Biscuits Platter-      $" << e << endl;

        cout << "\nAre there any addtional orders? 'Y' or 'N'\n" << endl;
        cin >> answer;
        if (answer == 'Y' || answer == 'y')

        {   //Display Choice

            cout << "\nYour choice?\n" << endl;
        }


         if (choice == 'A' || choice == 'a')
         {

            cout << "A: Earl Gray Tea and Biscuits" << a << endl;

        }


        if (choice == 'B' || choice == 'b')
        {

            cout << "B: Coffee and a blueberry scone" << b << endl;

        }


      if (choice == 'C' || choice == 'c')
        {

            cout << "A: Earl Gray Tea and Biscuits" << c << endl;

        }


         if (choice == 'D' || choice == 'd')
        {

            cout << "D: Coffee and a Muffin" << d << endl;

        }


        if (choice == 'E' || choice == 'e')
        {

            cout << "E: The Assorted Tea, Scones, and Biscuits Platter" << e << endl;

        }

        else if (answer == 'N' || answer == 'n')
        {
            cin >> totalprice;
            cout << "The final bill for today is ";
        }

        else //Displaying error message
        {
            cout << "Invalid input";
        }

    } while (answer != 'Y' && answer != 'y');

} 

2 个答案:

答案 0 :(得分:1)

变量选择应该是一个字符,你必须添加cin来接收选择的输入

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

    double a = 5.99, b = 4.99, c = 4.99, d = 5.99, e = 9.99, totalprice;
    const double TAX = 0.13;
    char choice;
    char answer;

    do
    {

        cout << "\nGood day! Welcome to The Bakery! What would you like today?\n";
        cout << "\nMenu\n                                                        Price"<< endl;
        cout << "A: Earl Gray Tea and Biscuits  -                        $" << a << endl;
        cout << "B: Coffee and a blueberry scone -                       $" << b << endl;
        cout << "C: Espresso and a tea biscuit -                         $" << c << endl;
        cout << "D: Coffee and a Muffin-                                 $" << d << endl;
        cout << "E: The Assorted Tea, Scones, and Biscuits Platter-      $" << e << endl;
cin >> choice;
        cout << "\nAre there any addtional orders? 'Y' or 'N'\n" << endl;
        cin >> answer;
        if (answer == 'Y' || answer == 'y')

        {   //Display Choice

            cout << "\nYour choice?\n" << endl;
        }


         if (choice == 'A' || choice == 'a')
         {

            cout << "A: Earl Gray Tea and Biscuits" << a << endl;

        }


        if (choice == 'B' || choice == 'b')
        {

            cout << "B: Coffee and a blueberry scone" << b << endl;

        }


      if (choice == 'C' || choice == 'c')
        {

            cout << "A: Earl Gray Tea and Biscuits" << c << endl;

        }


         if (choice == 'D' || choice == 'd')
        {

            cout << "D: Coffee and a Muffin" << d << endl;

        }


        if (choice == 'E' || choice == 'e')
        {

            cout << "E: The Assorted Tea, Scones, and Biscuits Platter" << e << endl;

        }

        else if (answer == 'N' || answer == 'n')
        {
            cin >> totalprice;
            cout << "The final bill for today is ";
        }

        else //Displaying error message
        {
            cout << "Invalid input";
        }

    } while (answer != 'Y' && answer != 'y');

} 

答案 1 :(得分:0)

在你做的时候忘了接受你的选择。这样做会更简单。

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

int main() {

  double a = 5.99, b = 4.99, c=4.99, d=5.99, e=9.99, totalprice;
  const double tax = 0.13;
  char answer;
  char choice;

然后你会输出你的菜单,并询问是否有任何额外的订单。并接受答案输入。

do {
  if(answer == 'Y' || 'y') {
    cout << "Your choice?" << endl;
    cin <<  choice
    if(choice == 'A' || choice == 'a') {
      cout << "A: Earl Gray Tea and Biscuits" << a << endl;
      totalprice = totalprice + a;
    } else if(choice == 'B' || choice == 'b') {
      cout << "B: Coffee and a blueberry scone" << b << endl;
      totalprice = totalprice + b;
    } else if(choice == 'C' || choice == 'c') {
      cout << "C: Espresso and a tea biscuit" << c << endl;
      totalprice = totalprice + c;
    } else if(choice == 'D' || choice == 'd') {
      cout << "D: Coffee and a Muffin " << d << endl;
      totalprice = totalprice + d;
    } else if(choice == 'E' || choice == 'e') {
      cout << "E: The Assorted Tea, Scones, and Biscuits Platter " << e << endl;
      totalprice = totalprice + e;
  }
  cout<< "\Are there any additional orders? Y or N << endl;
  cin << answer;
} while(answer != 'N' || answer !='n');
totalprice = totalprice * tax;
cout << "The final bill for today is $" << totalprice << endl;
相关问题