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