mercredi 10 juillet 2019

How Do I Properly Count User Inputs Using if-then Statements in C++

I am attempting to write program for homework which among other things keeps track of the number of certain kinds of snacks that are purchased during a visit to a movie theater. It first presents the user with the list of variable items and is supposed to display the number of each item purchased once the user has finished inputting the purchased items. Although my code does compile, the purchased number of most of the items that the program displays are not what I am hoping to see based on my inputs.

I am not sure how to modify the code to solve this issue, as for some reason the value of the softDrink variable seems to be stored and displayed, but this is not the case for any of the other variables.

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

In its current state, this code compiles without any errors despite not functioning as intended.

Aucun commentaire:

Enregistrer un commentaire