I promise I searched for the answer to this on my own, but I'm just having no luck. This is an assignment for school. It's supposed to continue looping and adding up total group admission prices, and it's supposed to exit when the user enters 0. I've got the main part working, but when I enter zero, the program outputs a price for it, throwing off the total price. I'm not getting any compiler errors, I'm just not wrapping my head around why this isn't working. Thanks in advance!
You enter the age at the start of the loop, this cout code happens at the end of the loop, and then a decision to contine the loop or not is made.
The loop doesn't stop the instant age becomes zero. It stops if age is zero when the check is made, and the check is made once after each time the loop is gone around.
I'm also not getting why the else statement is executed if the other two are not chosen.
That's how if-else statements work. A final else statement will be executed if none of the previous if statements were.
1 2 3 4 5 6 7 8 9 10 11 12
if (some_condition)
{
// do something
}
elseif (some_other_condtion)
{
// do something else
}
else
{
// This will ALWAYS happen if none of the above happened
}
If you don't want that, don't make the final statement an else; make it another else if.
1 2 3 4 5 6 7 8 9 10 11 12
if (some_condition)
{
// do something
}
elseif (some_other_condtion)
{
// do something else
}
elseif (some_other_other_condition)
{
// This will only happen if neither of the first two happened AND (some_other_other_condition) is true
}
According to the assignment, the program should acknowledge the 0 age, then exit the loop.
Well then why not do what the assignment tells you to?
1 2 3 4 5 6 7 8
cout << "Enter the person's age: ";
cin >> age;
cout << "The age you entered is " << age << endl;
if (age == 0)
{
cout << "I acknowledge the 0 age";
break;
}
Okay, I got it to work, but not in the most ideal way. I'm going to lose some points on this assignments. Instead of having the two statements at the end of the if/else structure, I just repeated those two lines on every if/else and it worked. My instructor is not a fan of having code repeating throughout the program but I am just at a loss. Thank you for the help!
do
{
cout << "Enter the person's age: ";
cin >> age;
cout << "The age you entered is " << age << endl;
if( age == 0)
{
cout << "I acknowledge the 0 age";
break;
}if (age < 2 && age > 0)
{
ticket = babyAdmission;
groupPriceSoFar = ticket + groupPriceSoFar;
}
elseif (2 <= age && age < 10)
{
ticket = childAdmission;
groupPriceSoFar = ticket + groupPriceSoFar;
}
elseif (age >= 10 && age <60)
{
ticket = adultAdmission;
groupPriceSoFar = ticket + groupPriceSoFar;
}
else
{
ticket = seniorAdmission;
groupPriceSoFar = ticket + groupPriceSoFar;
}
cout << "The price for this person is $" << ticket << endl;
cout << "The price for the group so far is $" << groupPriceSoFar << endl;
}
while (age != 0);
cout << "You entered zero. I won't ask for any more ages." << endl;
cout << "The total price for this group including tax is $" << groupPriceSoFar * stateTax;
return 0;
}
Tell me, what happens if someone enters "beans" or "-10" as the age?