所有'如果'语句立即执行

时间:2014-09-26 02:40:12

标签: c++ debugging if-statement cin

对于一个介绍C ++编程类,我必须设计一个程序,它接受用户的订单和订单的数量,但是我的代码在使用if语句时一次执行它们而不是在输出中一次执行一个。在完成第一项任务后,它没有对他们想要的初始质量的数量进行cin输入。我对他们对问题的回答宣布了不同的陈述,第一反应'但它仍然执行所有这些。

#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;
}

1 个答案:

答案 0 :(得分: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;
}

正如Paul Griffiths已经指出的那样,有一种更好的设计方法。

相关问题