2014-09-26 100 views
0

對於介紹性的C++編程類,我不得不設計一個程序來獲取用戶訂單和它們的訂單數量,但是我的代碼在使用if語句時同時執行,在輸出中一次一個。在完成第一項任務後,它不會接受cin輸入,以瞭解他們想要的初始質量。我對他們對「第一反應」問題的回答聲明瞭不同的陳述,但它仍然執行所有這些問題。一次執行所有'if'語句

#include "stdafx.h" 
#include <iostream> 

using namespace std; 

int main() 
{ 
    char response; 
    char secondresponse; 
    char thirdresponse; 

    int firstquantity; 
    int secondquantity; 
    int thirdquantity; 

    //The initial first step of showing the customer what is avaliable for purchase 
    cout << "Step 1" << endl; 
    cout << "Desserts avaliable for purchase:" << endl; 
    cout << "Ice Cream" << endl; 
    cout << "Creme Puffs" << endl; 
    cout << "Chocolate Cake" << endl; 

    //The customer chooses what they want to buy and also the quantity 
    cout << "Step 2" << endl;  
    //This is Step 2a  
    cout << "Would you like to purchase a tub of ice cream?" << endl; 
    cin >> response; 
    if (response == 121) 
    { 
     cout << "How many would you like?" << endl; 
     cin >> firstquantity; 
    } 
    else if (response != 121) 
     cout << "Would you like to purchase creme puffs?" << endl; 

    //This is step 2b 
    cout << "Would you like to purchase creme puffs?" << endl; 
    cin >> secondresponse; 
    if (secondresponse == 121) 
    { 
     cout << "How many would you like?"; 
     cin >> secondquantity; 
    } 
    //This is step 2c 
    cout << "Would you like to purchase some cake?" << endl; 
    cin >> thirdresponse; 
    if (thirdresponse == 121) 
    { 
     cout << "How many cakes would you like?" << endl; 
     cin >> thirdquantity; 
    }  

    system("PAUSE");  
    return 0; 
} 
+0

你使用什麼編譯器? – 2014-09-26 02:46:12

+0

@TritonMan我正在使用微軟的Visual Studio 2010 – renardeau 2014-09-26 02:48:07

+0

也可以使用std :: string代替輸入類型,並比較==「y」 – 2014-09-26 02:54:05

回答

0

這是你想要的嗎?

  • 如果第一個選擇是'y',那麼詢問數量並結束。
  • 否則如果第一選擇不是'y',則移動到第二個
  • 等等。

如果是這樣,看看這個代碼:

int main() 
{ 
    char response; 
    char secondresponse; 
    char thirdresponse; 

    int firstquantity = 0; 
    int secondquantity= 0; 
    int thirdquantity = 0; 

    //The initial first step of showing the customer what is avaliable for purchase 
    cout << "Step 1" << endl; 
    cout << "Desserts avaliable for purchase:" << endl; 
    cout << "Ice Cream" << endl; 
    cout << "Creme Puffs" << endl; 
    cout << "Chocolate Cake" << endl; 

    //The customer chooses what they want to buy and also the quantity 
    cout << "Step 2" << endl; 

    //This is Step 2a  
    cout << "Would you like to purchase a tub of ice cream?" << endl; 
    cin >> response; 
    if (response == 121) 
    { 
     cout << "How many would you like?" << endl; 
     cin >> firstquantity;  
    } 
    else 
    { 
     //This is step 2b 
     cout << "Would you like to purchase creme puffs?" << endl; 
     cin >> secondresponse; 
     if (secondresponse == 121) 
     { 
      cout << "How many would you like?"; 
      cin >> secondquantity; 
      cout << "Ordered creme puffs: " << secondquantity; 
     } 
     else 
     { 
      //This is step 2c 
      cout << "Would you like to purchase some cake?" << endl; 
      cin >> thirdresponse; 
      if (thirdresponse == 121) 
      { 
       cout << "How many cakes would you like?" << endl; 
       cin >> thirdquantity; 
      } 
     } 
    } 
    cout << "Ordered Ice Cream: " << firstquantity << endl; 
    cout << "Ordered creme puffs: " << secondquantity << endl; 
    cout << "Ordered Cakes: " << thirdquantity << endl; 
    system("PAUSE"); 
    return 0; 
} 

正如保羅·格里菲思已經指出的,有設計的更好的方法。