2016-04-05 25 views
2

這是我的任務:編寫一個使用枚舉類型銷售爆米花和飲料的程序。您必須在您的 程序中使用以下內容: 枚舉大小{小,中,大,大}; 尺寸popcornSize,drinkSize; 你應該創建一個菜單,要求用戶選擇他們想要什麼大小的飲料,並選擇他們想要的爆米花大小。那麼你應該打印出飲料和爆米花的總成本。使用枚舉類型C++類編寫銷售爆米花和飲料的程序

價格: 爆米花小= 1.25,介質= 2.25,大型= 3.50,巨型= 4.25 蘇打小= 1.50,介質= 2.50,大型= 3.75,巨型= 4.50

這是我的代碼:

int main() 
{ 
    enum sizes { SMALL, MEDIUM, LARGE, JUMBO }; 
    sizes popcornSize, drinkSize; 

    double sp=1.25, mp=2.25, lp=3.50, jp=4.25, TOTALp, TOTALs, ss=1.50,   ms=2.50, ls=3.75, js=4.50 ; 
    char choice, answer, Psize, Ssize; 
    int how_many; 
    cout << "Hello I am selling popcorn and sodas, would you like to buy some? type yes or no please." << endl; 
    cin >> choice; 
    if (choice == 'yes') 
    { 
     cout << "Great, what would you like popcorn or soda? Type P for popcorn and S for soda." << endl; 
     cin >> answer; 
     if (answer = 'P') 
     { 
      cout << "what size popcorn would you like (type s for small, m for medium, l for large or j for jumbo) and how many (type a single number)?" << endl; 
      cin >> Psize >> how_many; 
      if (Psize = 's') 
      { 
       TOTALp = sp*how_many; 
       cout << "Okay you total is: " << TOTALp << endl; 
      } 
      else if (Psize = 'm') 
      { 
       TOTALp = mp*how_many; 
       cout << "Okay you total is: " << TOTALp << endl; 
      } 
      else if (Psize = 'l') 
      { 
       TOTALp = lp*how_many; 
       cout << "Okay you total is: " << TOTALp << endl; 
      } 
      else if (Psize = 'j') 
      { 
       TOTALp = jp*how_many; 
       cout << "Okay you total is: " << TOTALp << endl; 
      } 


      else if (answer = 'S') 
      { 

       cout << "what size soda would you like (type small, medium, large or jumbo) and how many (type a single number)?" << endl; 
       cin >> Ssize >> how_many; 
       if (Ssize = 's') 
       { 
        TOTALs = ss*how_many; 
        cout << "Okay you total is: " << TOTALs << endl; 
       } 
       else if (Ssize = 'm') 
       { 
        TOTALs = ms*how_many; 
        cout << "Okay you total is: " << TOTALs << endl; 
       } 
       else if (Ssize = 'l') 
       { 
        TOTALs = ls*how_many; 
        cout << "Okay you total is: " << TOTALs << endl; 
       } 
       else if (Ssize = 'j') 
       { 
        TOTALs = js*how_many; 
        cout << "Okay you total is: " << TOTALs << endl; 
       } 

      } 
      cout << "Thanks for buying come again soon." << endl; 
     } 

     } 
     else if (choice == 'no') 
      cout << "Okay have a great day!" << endl; 
    return 0; 
} 

我需要幫助在程序中實現枚舉,以及如何將所有這些if/else語句放入交換機結構中。此外,由於某種原因,當用戶輸入他們的選擇後,我運行我的程序時,程序結束,我不知道爲什麼。

回答

2

你的程序結束了,因爲選擇永遠不是'是'也不是'不'。作爲選擇是一個字符,它可以是一個字符。將'yes'改爲'y'並將'no'改爲'n',它應該可以工作。

我給你一個如何編寫switch語句的例子,以便你可以使用它來調整代碼的其餘部分。

#include "stdio.h" 
#include "iostream" 

using namespace std; 

int main() 
{ 
    enum sizes { SMALL, MEDIUM, LARGE, JUMBO }; 
    sizes popcornSize, drinkSize; 

    double sp=1.25, mp=2.25, lp=3.50, jp=4.25, TOTALp, TOTALs, ss=1.50,   ms=2.50, ls=3.75, js=4.50 ; 
    char choice, answer, Psize, Ssize; 
    int how_many; 
    cout << "Hello I am selling popcorn and sodas, would you like to buy some? type yes or no please." << endl; 
    cin >> choice; 
    switch(choice) 
    { 
    case 'y': 
     cout << "Great, what would you like popcorn or soda? Type P for popcorn and S for soda." << endl; 
     cin >> answer; 
     if (answer = 'P') 
     { 
      cout << "what size popcorn would you like (type s for small, m for medium, l for large or j for jumbo) and how many (type a single number)?" << endl; 
      cin >> Psize >> how_many; 
      if (Psize = 's') 
      { 
       TOTALp = sp*how_many; 
       cout << "Okay you total is: " << TOTALp << endl; 
      } 
      else if (Psize = 'm') 
      { 
       TOTALp = mp*how_many; 
       cout << "Okay you total is: " << TOTALp << endl; 
      } 
      else if (Psize = 'l') 
      { 
       TOTALp = lp*how_many; 
       cout << "Okay you total is: " << TOTALp << endl; 
      } 
      else if (Psize = 'j') 
      { 
       TOTALp = jp*how_many; 
       cout << "Okay you total is: " << TOTALp << endl; 
      } 


      else if (answer = 'S') 
      { 

       cout << "what size soda would you like (type small, medium, large or jumbo) and how many (type a single number)?" << endl; 
       cin >> Ssize >> how_many; 
       if (Ssize = 's') 
       { 
        TOTALs = ss*how_many; 
        cout << "Okay you total is: " << TOTALs << endl; 
       } 
       else if (Ssize = 'm') 
       { 
        TOTALs = ms*how_many; 
        cout << "Okay you total is: " << TOTALs << endl; 
       } 
       else if (Ssize = 'l') 
       { 
        TOTALs = ls*how_many; 
        cout << "Okay you total is: " << TOTALs << endl; 
       } 
       else if (Ssize = 'j') 
       { 
        TOTALs = js*how_many; 
        cout << "Okay you total is: " << TOTALs << endl; 
       } 

      } 
      cout << "Thanks for buying come again soon." << endl; 
     } 
    break; 
    case 'n': 
     cout << "Okay have a great day!" << endl; 
    break; 
    default: 
     cout << "I dont understand...!" << endl; 
    break; 
    } 
    return 0; 
} 
+0

非常感謝!然而,我仍然堅持如何將枚舉數據實現到我的代碼中,關於爆米花和蘇打水的尺寸和價格 –

+0

問題是,您希望用戶輸入什麼內容?例如,您可以編寫一個switch case語句,該語句接受用戶輸入並設置每種情況下的枚舉值。 – Paraboloid87