### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293`` `````` #include 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 << "=========================" <> 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< 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; } ``````
 ``12`` ``````int yougest = 0; int oldest = 0``````
Line 14:
`int average = 0, sum = 0, counter = 0, youngest=99, oldest=0;`
 ``123456789101112`` `````` 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,

 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