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;
}
你使用什麼編譯器? – 2014-09-26 02:46:12
@TritonMan我正在使用微軟的Visual Studio 2010 – renardeau 2014-09-26 02:48:07
也可以使用std :: string代替輸入類型,並比較==「y」 – 2014-09-26 02:54:05