如何在C ++中使用if-then语句正确计算用户输入

时间:2019-07-11 02:50:23

标签: c++ if-statement math

我正在尝试编写家庭作业程序,其中除其他事项外,还要跟踪在电影院参观期间购买的某些小吃的数量。它首先向用户显示可变商品的列表,并应在用户完成输入已购买商品后显示每个已购买商品的数量。尽管我的代码确实可以编译,但是根据我的输入,程序显示的大多数项目的购买数量并不是我希望看到的。

我不确定如何修改代码来解决此问题,因为出于某种原因,似乎会存储并显示softDrink变量的值,但其他变量不是这种情况。

#include <iostream>

using namespace std;

int main()
{
//Declare the mathematical variables
int age = 0, average = 0, youngest = 0, oldest = 0, sum = 0, counter = 0;
//Declare the age variables
int ageGroup1 = 0, ageGroup2 = 0, ageGroup3 = 0, ageGroup4 = 0, ageGroup5 
= 0;
//Declare the snack variables
int snack = 0, softDrink = 0, popcorn = 0, nachos = 0, softPop = 0, 
softNachos = 0, organic = 0;

//Display the menu
cout << "==========================" << endl;
cout << "  THEATER STATS PROGRAM" << endl;
cout << "==========================\n" << endl;
cout << "Movie theater snacks available for purchase" << endl;
cout << "==========================================" << endl;
cout << "1 - Soft Drink (such as Coca Cola, ICCEE, Mineral Water etc...)" 
<< endl;
cout << "2 - Popcorn" << endl;
cout << "3 - Nachos" << endl;
cout << "4 - Soft drink & Popcorn" << endl;
cout << "5 - Soft drink & Nachos" << endl;
cout << "6 - Organic and Gluten-free snacks" << endl;
cout << "7 - None" << endl;
cout << "==========================================" << endl;

//Prompt a series of inputs to determine how many attendees fall into 
each age group and what snacks they will buy
cout << "Enter age of attendee (-1 to quit): ";
cin >> age;

while (age != -1)
{
    //Prompt user input for the purchased snacks
    cout << "Movie theater snack purchased. (Select items 1 - 7): ";
    cin >> snack;

    //Keep track of youngest and oldest attendees
    if (age < youngest)
        youngest = age;
    if (age > oldest)
        oldest = age;

    //Sum up all the inputted ages
    sum = sum + age;
    counter++;

    //Store the values for age inputs into different groups
    if (age >= 0 && age <= 18)
         ageGroup1++;
    else if (age >= 19 && age <= 30)
        ageGroup2++;
    else if (age >= 31 && age <= 40)
        ageGroup3++;
    else if(age >= 41 && age <= 60)
        ageGroup4++;
    else if (age >= 61)
        ageGroup5++;

    //Store the values for snack inputs
    if (snack == 1 || snack == 4 || snack == 5)
        softDrink++;
    else if (snack == 2 || snack == 4)
        popcorn++;
    else if (snack == 3 || snack == 5)
        nachos++;
    else if (snack == 4)
        softPop++;
    else if (snack == 5)
        softNachos++;
    else if (snack == 6)
        organic++;
    //Inform the user when they have inputted an invalid snack value
    while (snack < 1 || snack > 7)
    {
            cout << "Invalid selection, please choose from 1 - 7." << 
            endl;
            cout << "Movie theater snack purchased. (Select items 1 - 7): 
            ";
            cin >> snack;
    }

    //Prompt the user to continue entering age values and keep track of 
    the number of inputted ages
    cout << "Enter age of attendee (-1 to quit): ";
    cin >> age;
    counter++;
}

//Calculate the average age of the attendees
average = sum / (counter - 2);

//Display the results of the user's inputs
cout << "==================================" << endl;
cout << "  THEATER STATS PROGRAM RESULTS" << endl;
cout << "==================================\n" << endl;
cout << "Age 0  to 18:    " << ageGroup1 << endl;
cout << "Age 19 to 30:    " << ageGroup2 << endl;
cout << "Age 31 to 40:    " << ageGroup3 << endl;
cout << "Age 41 to 60:    " << ageGroup4 << endl;
cout << "Over 60:         " << ageGroup5 << "\n" << endl;
cout << "The average age was " << average << endl;
cout << "The youngest person in attendance was " << youngest << endl;
cout << "The oldest person in attendance was " << oldest << endl;
cout << "\nTheater Concession Stand sales" << endl;
cout << "==================================" << endl;
cout << "Soft Drink (such as Coca Cola, ICCEE, Mineral Water etc.): " << 
softDrink << endl;
cout << "Popcorn: " << popcorn << endl;
cout << "Nachos: " << nachos << endl;
cout << "Soft Drink & Popcorn: " << softPop << endl;
cout << "Soft Drink & Nachos: " << softNachos << endl;
cout << "Organic and Gluten-free snacks: " << organic << endl;

return 0;
}

在当前状态下,即使未按预期运行,该代码也不会出现任何错误。

1 个答案:

答案 0 :(得分:0)

获得年龄后,初始化年龄最大和最小的那个年龄。另外,由于推荐了@Nico Schertler,我还删除了else if语句。

    cin >> age;
    youngest = age;
    oldest = age;

    while (age != -1)
    {
        //Prompt user input for the purchased snacks
        cout << "Movie theater snack purchased. (Select items 1 - 7): ";
        cin >> snack;

        //Keep track of youngest and oldest attendees
        if (age < youngest)
            youngest = age;
        if (age > oldest)
            oldest = age;

        //Sum up all the inputted ages
        sum = sum + age;
        counter++;

        //Store the values for age inputs into different groups
        if (age >= 0 && age <= 18)
            ageGroup1++;
        if (age >= 19 && age <= 30)
            ageGroup2++;
        if (age >= 31 && age <= 40)
            ageGroup3++;
        if (age >= 41 && age <= 60)
            ageGroup4++;
        if (age >= 61)
            ageGroup5++;

        //Store the values for snack inputs
        if (snack == 1 || snack == 4 || snack == 5)
            softDrink++;
        if (snack == 2 || snack == 4)
            popcorn++;
        if (snack == 3 || snack == 5)
            nachos++;
        if (snack == 4)
            softPop++;
        if (snack == 5)
            softNachos++;
        if (snack == 6)
            organic++;
        //Inform the user when they have inputted an invalid snack value
        while (snack < 1 || snack > 7)
        {
            cout << "Invalid selection, please choose from 1 - 7." <<
                endl;
            cout << "Movie theater snack purchased. (Select items 1 - 7): ";
                cin >> snack;
        }

        //Prompt the user to continue entering age values and keep track of 
    /*  the number of inputted ages*/
            cout << "Enter age of attendee (-1 to quit): ";
        cin >> age;
        counter++;
    }