Help on a Theatre statistics program

hey guys, i'm new to programming and as such im not very bright when it comes to code. I've been working on a program that asks for "theater statistics" including age of all attendees, and what "snack they chose from the snack bar given 8 choices, and given the ages determine the lowest and highest aged people. Essentially I got to the part of the code where it asks for me to find the highest and lowest age people but every time i run my code it gives me the highest and a zero for my lowest.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
using namespace std;
int main()
{
    int  age, age1 = 0, age2 = 0, age3=0, age4=0, age5=0, sum = 0,  counter = 0, low, high;
    double average = 0;

    cout << "please enter age of attendees (-1 to quit): ";
    cin>>age;



    while (age != -1)
    {

        if (counter ==0)
        {
            high = age;
            low  = age;
        }
        else
        {
            if  (age < low &&  age != -1)
                 low = age;

            else if
                (age > high && age != -1)
                high = age;
        }


        counter++;
        sum=sum+age;
           cout << "please enter age of attendees (-1 to quit): ";
           cin>>age;

        if (age>=0 && age<=18)
        {
         age1++;
        }

        if (age>=19 && age<=30)
        {
            age2++;
        }
        if (age>=31 && age<=40)
        {
            age3++;
        }
        if (age>=41 && age<=60)
        {
            age4++;
        }
             if (age>60)
        {
            age5++;
        }



    }


    cout << "The total number of attendees is " << counter << endl;
    average=sum/counter;


    cout << "The average age of the attendees is :" << average << endl;
    cout << "Lopwest age was" << low << endl;
    cout << "highest age was" << high << endl;




    cout << "age group 1 " << age1 <<endl;
    cout << "age group 2 " << age2 <<endl;
    cout << "age group 3 " << age3 <<endl;
    cout << "age group 4 " << age4 <<endl;
    cout << "age group 5 " << age5 <<endl;







    return 0;
Last edited on
Hello Splaterfix,



PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


You are missing the closing brace of main.

It will take a few minutes to fix your code so I can read it and test it.

Andy
Thanks Andy, i formatted the code now so it should display correctly now. I got the code to finally tell me the lowest and highest input ages, but now im facing a new titan. The next step is to include a theatre concessions list to ask the user to choose from as they input the ages so the console would look something like this
==========================
THEATER STATS PROGRAM
==========================

Movie theater snacks available for purchase
==========================================
1 - Soft Drink (such as Coca Cola, ICCEE, Mineral Water etc...)
2 - Popcorn
3 - Nachos
4 - Soft drink & Popcorn
5 - Soft drink & Nachos
6 - Organic and Gluten-free snacks
7 - None
==========================================

Enter age of attendee (-1 to quit): 34
Movie theater snack purchased. (Select items 1 - 7):4
--------------------------
Enter age of attendee (-1 to quit): 16
Movie theater snack purchased. (Select items 1 - 7):5
--------------------------
Enter age of attendee (-1 to quit): 68
Movie theater snack purchased. (Select items 1 - 7):12
Invalid selection, please choose from 1 - 7
Movie theater snack purchased. (Select items 1 - 7):6
--------------------------
Enter age of attendee (-1 to quit): 53
Movie theater snack purchased. (Select items 1 - 7):6
--------------------------
Enter age of attendee (-1 to quit): 39
Movie theater snack purchased. (Select items 1 - 7):1
--------------------------
Enter age of attendee (-1 to quit): 23
Movie theater snack purchased. (Select items 1 - 7):2
--------------------------
Enter age of attendee (-1 to quit): 21
Movie theater snack purchased. (Select items 1 - 7):3
--------------------------
Enter age of attendee (-1 to quit): 21
Movie theater snack purchased. (Select items 1 - 7):4
--------------------------
Enter age of attendee (-1 to quit): -1

==================================
THEATER STATS PROGRAM RESULTS
==================================

age 0 to 18: 1
age 19 to 30: 3
age 31 to 40: 2
age 41 to 60: 1
over 60: 1

The average age was 34
The youngest person in attendance was 16
The oldest person in attendance was 68

Theater Concession Stand sales
==================================
Soft Drink (such as Coca Cola, ICCEE, Mineral Water etc.): 1
Popcorn: 1
Nachos: 1
Soft drink & Popcorn: 2
Soft drink & Nachos: 1
Organic and Gluten-free snacks: 2

Process returned 0 (0x0) execution time : 169.589 s
Press any key to continue.


while ,ine loos like this

please enter age of attendees (-1 to quit): 34
please enter age of attendees (-1 to quit): 16
please enter age of attendees (-1 to quit): 68
please enter age of attendees (-1 to quit): 53
please enter age of attendees (-1 to quit): 39
please enter age of attendees (-1 to quit): 23
please enter age of attendees (-1 to quit): 21
please enter age of attendees (-1 to quit): -1
The total number of attendees is 7
The average age of the attendees is :36
Lowest age was: 16
highest age was: 68
age group 1 1
age group 2 2
age group 3 1
age group 4 1
age group 5 1

Process returned 0 (0x0) execution time : 18.264 s
Press any key to continue.
Hello Splaterfix,

Much better.

I did some work on your code. See what you think of this while I consider the rest:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <iomanip>  // <--- Added.

//using namespace std;  // <--- Best not to use.

int main()
{
	int age{}, age1{}, age2{}, age3{}, age4{}, age5{}, sum{}, counter{}, low{}, high{};  // <--- Initialized variables to zero.
	double average{};

	while (age != -1)
	{
		if (counter == 0)
		{
			high = age;
			low = 100;  // <--- Changed.
		}
		else
		{
			if (age < low && age != -1)
				low = age;

			if (age > high && age != -1)  // <--- Changed.
				high = age;
		}

		std::cout << "\nplease enter age of attendees (-1 to quit): ";
		std::cin >> age;

		if (age == -1)  // <--- Added. Keeps counter from being one to many.
			continue;

		counter++;
		sum += age;  // <--- Changed.

		if (age >= 0 && age <= 18)
			age1++;

		if (age >= 19 && age <= 30)
			age2++;

		if (age >= 31 && age <= 40)
			age3++;

		if (age >= 41 && age <= 60)
			age4++;

		if (age > 60)
			age5++;
	}  //  End while loop.

	std::cout << std::fixed << std::showpoint << std::setprecision(2);  // <--- Added.

	std::cout << "\nThe total number of attendees is " << counter << std::endl;
	average = static_cast<double>(sum) / counter;  // <--- Changed. Otherwise it it integer division.


	std::cout << "\nThe average age of the attendees is: " << average << std::endl;
	std::cout << "Lowest age was: " << low << std::endl;
	std::cout << "highest age was: " << high << std::endl;




	std::cout << "\nage group 1 " << age1 << std::endl;
	std::cout << "age group 2 " << age2 << std::endl;
	std::cout << "age group 3 " << age3 << std::endl;
	std::cout << "age group 4 " << age4 << std::endl;
	std::cout << "age group 5 " << age5 << std::endl;

	return 0;
}


Hope that helps,

Andy
hey andy, thanks for the help. its coming together bit by bit. One thing im stuck on is why my age groups are not functioning correctly.

please enter age of attendees (-1 to quit): 34
please enter age of attendees (-1 to quit): 16
please enter age of attendees (-1 to quit): 68
please enter age of attendees (-1 to quit): 53
please enter age of attendees (-1 to quit): 39
please enter age of attendees (-1 to quit): 23
please enter age of attendees (-1 to quit): 21
please enter age of attendees (-1 to quit): -1

The total number of attendees is 7
The average age of the attendees is :36.29
Lowest age was: 16
highest age was: 68
ages 0-18: 1
ages 19-30: 2
ages 31-40: 1
ages 41-60:1
ags 60 and up: 1

Process returned 0 (0x0) execution time : 20.551 s
Press any key to continue.

the age group form 31-40 should be 2 instead of 1 and im not to sure what I did wrong. It doesent count either 34 or 39?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int  age, age1 = 0, age2 = 0, age3=0, age4=0, age5=0, sum = 0,  counter = 0, low, high;
    double average = 0;

    cout << "please enter age of attendees (-1 to quit): ";
    cin>>age;


    while (age != -1)
    {

                    if (counter ==0)
                    {
                        high = age;
                        low  = age;
                    }
                    else
                    {
                        if  (age < low &&  age != -1)
                             low = age;

                        if
                            (age > high && age != -1)
                            high = age;
                    }


        counter++;
        sum+= age;
           cout << "please enter age of attendees (-1 to quit): ";
           cin>>age;

                    if (age>=0 && age<=18)
                    {
                        age1++;
                    }

                    if (age>=19 && age<=30)
                    {
                        age2++;
                    }
                    if (age>=31 && age<=40)
                    {
                        age3++;
                    }
                    if (age>=41 && age<=60)
                    {
                        age4++;
                    }
                         if (age>=61)
                    {
                        age5++;
                    }



    }
    cout<<fixed<<showpoint<<setprecision(2)<<endl;

    cout << "The total number of attendees is " << counter << endl;
             average=static_cast<double>(sum)/counter;


    cout << "The average age of the attendees is :" << average << endl;
    cout << "Lowest age was: "  <<  low << endl;
    cout << "highest age was: " << high << endl;




    cout << "ages 0-18: " << age1 <<endl;
    cout << "ages 19-30: " << age2 <<endl;
    cout << "ages 31-40: " << age3 <<endl;
    cout << "ages 41-60:"  << age4 <<endl;
    cout << "ags 60 and up: " << age5 <<endl;







    return 0;


}
Hello Splaterfix,

I am not sure about age group 3. When I used your numbers it worked fine for me. I will have to check into it tomorrow.

This is what I am using now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <iomanip>  // <--- Added.

//using namespace std;  // <--- Best not to use.

int main()
{
	constexpr size_t MAXSIZE{ 6 };
	int snacks[MAXSIZE]{};
	int age{}, age1{}, age2{}, age3{}, age4{}, age5{}, sum{}, counter{}, low{}, high{}, snack{};  // <--- Initialized variables to zero.
	double average{};


	while (age != -1)
	{
		system("cls");

		std::cout << "\n==========================";
		std::cout << "\nTHEATER STATS PROGRAM";
		std::cout << "\n==========================" << std::endl;

		if (counter == 0)
		{
			high = age;
			low = 100;  // <--- Changed.
		}
		else
		{
			if (age < low && age != -1)
				low = age;

			if (age > high && age != -1)  // <--- Changed.
				high = age;
		}

		std::cout << "\nMovie theater snacks available for purchase";
		std::cout << "\n==========================================";
		std::cout << "\n1 - Soft Drink(such as Coca Cola, ICCEE, Mineral Water etc...)";
		std::cout << "\n2 - Popcorn";
		std::cout << "\n3 - Nachos";
		std::cout << "\n4 - Soft drink & Popcorn";
		std::cout << "\n5 - Soft drink & Nachos";
		std::cout << "\n6 - Organic and Gluten - free snacks";
		std::cout << "\n7 - None";
		std::cout << "\n==========================================" << std::endl;

		std::cout << "\nplease enter age of attendees (-1 to quit): ";
		std::cin >> age;

		if (age == -1)  // <--- Added. Keeps counter from being one to many.
			continue;

		std::cout << "\nPlease enter snack purchased: ";
		std::cin >> snack;

		switch (snack)
		{
			case 1:
				snacks[0]++;
				break;
			case 2:
				snacks[1]++;
				break;

			case 7:
				break;
		}

		counter++;
		sum += age;  // <--- Changed.

		if (age > 0 && age <= 18)
			age1++;

		if (age >= 19 && age <= 30)
			age2++;

		if (age >= 31 && age <= 40)
			age3++;

		if (age >= 41 && age <= 60)
			age4++;

		if (age > 60)
			age5++;
	}  //  End while loop.

	std::cout << std::fixed << std::showpoint << std::setprecision(2);  // <--- Added.

	std::cout << "\nThe total number of attendees is " << counter << std::endl;
	average = static_cast<double>(sum) / counter;  // <--- Changed. Otherwise it it interger division.


	std::cout << "\nThe average age of the attendees is: " << average << std::endl;
	std::cout << "Lowest age was: " << low << std::endl;
	std::cout << "highest age was: " << high << std::endl;

	std::cout << "\nage group 1 (1 to 18): " << age1 << std::endl;
	std::cout << "age group 2 (19 to 30): " << age2 << std::endl;
	std::cout << "age group 3 " << age3 << std::endl;
	std::cout << "age group 4 " << age4 << std::endl;
	std::cout << "age group 5 " << age5 << std::endl;

	std::cout << "\nTheater Concession Stand sales";
	std::cout << "\n==================================";
	std::cout << "\nSoft Drink(such as Coca Cola, ICCEE, Mineral Water etc.): " << snacks[0];
	std::cout << "\nPopcorn: " << snacks[1];

	return 0;
}

I really do not like using "system" anything, but for the moment it is quick and dirty to serve a purpose.

I added some code to deal with the snacks, but it needs finished.

Hope that helps for now,

Andy
hey andy, thanks alot. I appreciate it. people like yoiu are what give newbies like me hope. And im not sure why the age group three isnt working for me either. I dont see where I could have done something wrong. but once again thank you so much for the help
Hello Splaterfix,

You are welcome a lot.

And I'm not sure why the age group three isn't working for me either.

I am not sure why either. I have been trying all day and the closest problem is when I typed "35" when I wanted "53", but the output was correct for "age3" with a value of "3".

The if statements are your original code and I have yet to find anything wrong with them. I do have one suggestion for the first if statement: age >= 0. Zero may be a bit low here. You might want to consider say "5" to "8". It seems more reasonable.

I finished up some of the program today and it now produces this output. I believe this is what you are looking for. Or its something close to it.

The total number of attendees is 8

The average age of the attendees is: 35.63
Lowest age was: 16
highest age was: 68

 age group 1 (01 to 18): 1
 age group 2 (19 to 30): 2
 age group 3 (31 to 40): 2
 age group 4 (41 to 60): 1
 age group 5 (over  60): 1

Theater Concession Stand sales
==================================
Soft Drink(such as Coca Cola, ICCEE, Mineral Water etc.): 1
Popcorn: 1
Nachos: 1
Soft drink & Popcorn: 2
Soft drink & Nacho: 2
Organic and Gluten - free snacks: 0

 Press Enter to continue



Here is a tip to reduce the testing time:

I added these variables:
1
2
int ages[]{ 34, 16, 68, 53, 39, 23, 21, -1 };
int snacksPurched[7]{ 1, 2, 3, 4, 5, 4, 5};

The "7" refers to the number of ages not including the "-1". Since it is for testing I did not bother creating a constant variable. This way you do not have to type the same thing every time the program runs. One way of reducing typing mistakes. When finished testing Comment or remove these two lines. Comments work best in case you have to make changes or do more work.

You can add as many ages as yo want as long as it ends in "-1". Then add to the "snacksPurched" array a number to cover each age.

Farther down in the code I did this:
1
2
3
4
5
6
7
8
9
//std::cout << "\nplease enter age of attendees (-1 to quit): ";
//std::cin >> age;
age = ages[counter];
std::cout << "\nplease enter age of attendees (-1 to quit): " << age << std::endl;

//std::cout << "\nPlease enter snack purchased: ";
//std::cin >> snack;
snack = snacksPurched[counter];
std::cout << "\nPlease enter snack purchased: " << snack << std::endl;

Using something to clear the screen you are not likely to see the "cout" line unless you put a break point in the program. So even if you do not happen to see it during a run it still has its use.

When you are finished testing just reverse the comments for normal run.

Hope that helps,

Andy
Topic archived. No new replies allowed.