How do I make my selection statement not run in the loop after I put my sentinel to quit on my age input

//Finding smallest number in a series of user input of numbers
#include <iostream>
#include <cmath>
using namespace std;

int main()
{

//Main interface for theater statistics program
int menuChoice;
cout << " =======================" << endl;
cout << " THEATER STATS PROGRAM" << endl;
cout << " =======================" << endl;
cout << " " << 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;
cout << " " << endl;


//Variables for different types of placeholders
int age, selection, smallest = 1000, greatest = 0, age0_18 = 0;
int sel1 = 0, sel2 = 0, sel3 = 0, sel4 = 0, sel5 = 0, sel6 = 0, sel7 = 0;
int age19_30 = 0, age31_40 = 0, age41_60 = 0, over60 = 0, counter = 0, sum = 0;
cout << "Enter age of attendee (-1 to quit): ";
cin >> age; //priming need
cout << "Movie theater snack purchased. (Select items 1 - 7): ";
cin >> selection; //Second priming need
cout << "--------------------------" << endl;

smallest = greatest = age;
sum = sum + age;

//Process of the program
while(age != -1)
{
if(age < smallest)
{
smallest = age;
}

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

else if(age >= 19 && age <= 30)
{
age19_30++;
}

if(age >= 31 && age <= 40)
{
age31_40++;
}

else if(age >= 41 && age <= 60)
{
age41_60++;
}
if(age >= 60)
{
over60++;
}
if(selection == 1)
{
sel1++;
}
if(selection == 2)
{
sel2++;
}
if(selection == 3)
{
sel3++;
}
if(selection == 4)
{
sel4++;
}
if(selection == 5)
{
sel5++;
}
if(selection == 6)
{
sel6++;
}
if(selection == 7)
{
sel7++;
}
if(selection > 7)
{
cout << "Invalid selection, please choose from 1 - 7" << endl;
cout << "Movie theater snack purchased. (Select items 1 - 7): ";
cin >> selection;
cout << "--------------------------" << endl;
sel6++;
}

//prompt for next age input by user
cout << "Enter age of attendee (-1 to quit): ";
cin>> age;
sum+=age;
counter++;
cout << "Movie theater snack purchased. (Select items 1 - 7): ";
cin >> selection;
cout << "--------------------------" << endl;

}
cout << " " << 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: " << over60 << "\n" << endl;
cout << "The average was " << sum/counter << endl;
cout << "The youngest person in attendance was " << smallest << endl;
cout << "The oldest person in attendance was " << greatest << endl;
cout << " " << endl;
cout << "Theater Concession Stand sales" << endl;
cout << "==================================" << endl;
cout << "Soft Drink (such as Coca Cola, ICCEE, Mineral Water etc.): " << sel1 << endl;
cout << "Popcorn: " << sel2 << endl;
cout << "Nachos: " << sel3 << endl;
cout << "Soft drink & Popcorn: " << sel4 << endl;
cout << "Soft drink & Nachos: " << sel5 << endl;
cout << "Organic and Gluten-free snacks: " << sel6 << endl;

return 0;
}
After getting the value (cin>> age) you need to check to see if it is your ‘quit’ sentinel value before doing anything else with it.

But...

Sentinel values suck. Don’t use them if you can avoid them.
Many times professors design homework input requirements to use them...

Notice how you are prompting for age in two spots: once before the loop and once in the loop. Notice also how both times you are asking for the next loop. This is a good clue that you should instead ask right at the beginning of the loop, rather than elsewhere.

For dealing with user input, a little helper function is often very useful:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template <typename T>
bool ask( const std::string& prompt, T& result )
{
  std::cout << prompt;

  // Attempt to get any input. EOF or blank == user is done giving input
  std::string s;
  if (!getline( std::cin, s ) or s.empty()) return false;

  // Attempt to convert the input to the target object
  std::istringstream ss{ s };
  ss >> result >> std::ws;

  // Returns true if conversion was successful
  // and there was nothing else in input besides the requested item
  return ss.eof();
}

Now you can use it to help in your loop:

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
while (true)
{
  if (!ask( "Age of attendee? ", age ) or (age < 0))
    // Any invalid input == done
    break;

  if (!ask( "Item to purchase (1-7)? ", selection ) or (1 > selection) or (selection > 7)) 
    break;

  // If we get this far, the user has not failed to input the desired information.
  std::cout << "Movie theater snack successfully purchased.\n";

  // We can now use the input information to do stuff, like track age and sales statistics...

  smallest = std::min( smallest, age );
  ...

}

// At this point, all user input has been successfully entered.
// Print out your statistics.

std::cout
  << "Age  0 to 18: " << age0_18 << "\n"
  << "Age 19 to 30: " << age19_30 << "\n"
  ...
  << std::endl;

Hope this helps.
Registered users can post here. Sign in or register to post.