Help with Loops/If Elses

Hi, this is an assignment for class that I can't seem to get fully working. We just started a few weeks ago, so we can't use anything advanced. But basically I'm just trying to figure out the oldest and youngest age without having it register -1 as an age (we have to use -1 to escape the loop), everything works fine except the youngest and oldest ages. thanks in advance.

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

#include <iostream>
using namespace std;
int main()
{
//Declare needed variables
int age, snacks;
//age group counters
int age0_18 = 0;
int age19_30 = 0;
int age31_40 = 0;
int age41_60 = 0;
int ageover60 = 0;
int average = 0, sum = 0, counter = 0, youngest, oldest;

//Initial program information display

  cout << "=========================" <<endl;
  cout << " THEATER STATS PROGRAM" << endl;
  cout << "==========================" << endl<<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<<endl;

//Prompt for input
//An event control loop

while (age != -1)
{cout << "Enter age of attendee (-1 to quit): ";
 cin >> age;
 if (age != -1)
 {counter ++;
 sum += age; }
    if (age >= 0 && age <=18)
     age0_18 ++;
    else if(age >=19 && age <= 30)
     age19_30 ++;
    else if (age >=31 && age <=40)
     age31_40 ++;
    else if (age >= 41 && age <=60)
     age41_60 ++;
    else if (age > 61)
    ageover60 ++;
if (age != -1)
{cout << "Movie theater snack purchased. (Select items 1 - 7): ";
 cin >> snacks; }
 if (snacks >= 8 || snacks <= 0 && age != -1)
 {cout << "Invalid selection, please choose from 1-7" << endl;
 cout << "Movie theater snack purchased. (Select items 1 - 7): ";  //Makes snacks 1-7
 cin >> snacks; }
 cout << "-----------------------------------------------------" << endl;

}

    //Displaying results
    cout << endl;
    cout << "==================================" << endl;
    cout << "THEATER STATS PROGRAM RESULTS" << endl;
    cout << "==================================" << endl<<endl;

    cout << "Age 0 to 18:    " << age0_18 << endl;
    cout << "Age 19 to 30:   " << age19_30 << endl;
    cout << "Age 31 to 40:   " << age31_40 << endl;
    cout << "Age 41 to 60:   " << age41_60 << endl;
    cout << "Over 60:        " << ageover60 << endl;

    //Average, highest, oldest
    cout << endl;
    average = sum/ counter;
    cout << "The average age was " << average << endl;
    if (youngest > age && age > 0)
    youngest = age;
    cout << "The youngest person in attendence was " << youngest << endl;
    if (oldest < age)
    oldest = age;
    cout << "The youngest person in attendence was " << oldest << endl;






return 0;
}
Might want to start with editing youngest and oldest.
1
2
int yougest = 0;
int oldest = 0
Line 14:
int average = 0, sum = 0, counter = 0, youngest=99, oldest=0;
Lines 39..50 (was your 39..51):
1
2
3
4
5
6
7
8
9
10
11
12
 if (age > -1)
 {
     counter ++;
     sum += age;
          if (age <= 18) age0_18 ++;    // >= 0 filtered above
     else if (age <= 30) age19_30 ++;   // > 18 filtered above
     else if (age <= 40) age31_40 ++;   // and so on
     else if (age <= 60) age41_60 ++;
     else               ageover60 ++;
     if (age < youngest) youngest = age; 
     if (age > oldest)     oldest = age;
}

My try:
Enter age of attendee (-1 to quit): 23
Movie theater snack purchased. (Select items 1 - 7): 6
-----------------------------------------------------
Enter age of attendee (-1 to quit): 8
Movie theater snack purchased. (Select items 1 - 7): 7
-----------------------------------------------------
Enter age of attendee (-1 to quit): 66
Movie theater snack purchased. (Select items 1 - 7): 8
Invalid selection, please choose from 1-7
Movie theater snack purchased. (Select items 1 - 7): 5
-----------------------------------------------------
Enter age of attendee (-1 to quit): -1
-----------------------------------------------------

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

Age 0 to 18:    1
Age 19 to 30:   1
Age 31 to 40:   0
Age 41 to 60:   0
Over 60:        1

The average age was 32
The youngest person in attendence was 8
The youngest person in attendence was 66
Hello jakeb10,

MikeStg has a good idea.

It is partially what you are doing, but the bigger point is where you are doing it. You are trying to set the youngest and oldest after the while loop, but this should be done inside the while loop after you have entered an age.

In your original code you have else if (age > 61). So if you are exactly 61 you are not counted?? I believe what you should have is [code][else if (age > 60)/code].

What you have works, but MikeStg's example is much cleaner and shorter to accomplish the same outcome.

At the end of your code you could do with less blank lines and in the rest of your code a few blank lines would be a great help to readability.

Most of your variables are given a value when they are defined, but you missed a couple.

When dealing with a min and max, or in this case youngest and oldest, I have found that the min value more often works best when defined and set to a large number. This only stays a large number the first time through the loop. After that the "age" entered will either set youngest or leave it alone. The max value is just fine starting at zero.

I do not know if it is your IDE or the way you entered the code in some text editor, but watch your indenting. If this is from the IDE you will need to adjust the "tab" settings. Not knowing what you are using it is hard to explain what to change.

Your use of {}s is more than one style. You need to pick a style and use it consistently. https://en.wikipedia.org/wiki/Indentation_style For what is is worth I tend to like the "Allman" style and there are other names for the same style.

Hope that helps,

Andy
That worked, thank you guys so much! I know my indents are really confusing I've only been coding for less than a month, it's definitely something I have to get it better at.

Also, just curious, but why did changing the youngest variable to 99 instead of 0 result in an actual answer?
If youngest = 0 then someone would have to be younger than 0 to change the value of youngest
Hello jakeb10,

As I already said:

When dealing with a min and max, or in this case youngest and oldest, I have found that the min value more often works best when defined and set to a large number. This only stays a large number the first time through the loop. After that the "age" entered will either set youngest or leave it alone. The max value is just fine starting at zero.


When you think about it setting "youngest" to zero to start with the first time though the loop you enter 20. 20 is not less than zero so youngest never changes. The same will happen on successive passes of the loop.

If you set "youngest" to 99, 200 or even 1000 the first time through the loop when you enter 20 it will be less than any of those numbers and the value of "youngest" will change. On successive passes through the loop you will be comparing the age entered to the new value of "youngest" and if it is less then it will change otherwise nothing happens.

Hope that helps,

Andy
Topic archived. No new replies allowed.