do...while loop not behaving

closed account (1Ck93TCk)
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!

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

#include <iostream>

using namespace std;

const double seniorDiscount = 0.9;
const double stateTax = 1.07;

int main()
{
    int age;
    double babyAdmission = 1;
    double childAdmission = 10;
    double adultAdmission = 20;
    double seniorAdmission = adultAdmission * seniorDiscount;
    double ticket;
    double groupPriceSoFar = 0;

    cout << "This program calculates admission price for groups." << endl;

        do
        {
            cout << "Enter the person's age: ";
	        cin >> age;
	        cout << "The age you entered is " << age << endl;

	        if (age < 2 && age > 0)
	            {
	            ticket = babyAdmission;
	            groupPriceSoFar = ticket + groupPriceSoFar;
	            }

	            else if (2 <= age && age < 10)
	                {
	                ticket = childAdmission;
	                groupPriceSoFar = ticket + groupPriceSoFar;
                    }

                else if (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;
}
Last edited on
Well of course it does. Your do-while loop contains this, outside of any if statements involving the age:
1
2
            cout << "The price for this person is $" << ticket << endl;
            cout << "The price for the group so far is $" << groupPriceSoFar << endl;

and this code will always be exectued if neither of the other if statements were taken:
1
2
3
4
5
  else
                {
                ticket = seniorAdmission;
                groupPriceSoFar = ticket + groupPriceSoFar;
                  }


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.
Last edited on
closed account (1Ck93TCk)

Thank you, Repeater, for responding. I really appreciate the help.

I think I'm just not understanding this clearly.

I want these lines to execute only once per loop and I was trying to avoid repeating the same code for each if/else statement.

1
2
cout << "The price for this person is $" << ticket << endl;
cout << "The price for the group so far is $" << groupPriceSoFar << endl;


According to the assignment, the program should acknowledge the 0 age, then exit the loop.

I'm also not getting why the else statement is executed if the other two are not chosen.

1
2
3
4
5
else
                {
                ticket = seniorAdmission;
                groupPriceSoFar = ticket + groupPriceSoFar;
                  }


I'm super new to this, but I'm doing my best to learn!
Thank you!
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
}
else if (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
}
else if (some_other_condtion)
{
  // do something else
}
else if (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;
}
Last edited on
closed account (1Ck93TCk)
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!
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
        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;
	            }

	            else if (2 <= age && age < 10)
	                {
	                ticket = childAdmission;
	                groupPriceSoFar = ticket + groupPriceSoFar;
                    }

                else if (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?
Last edited on
Topic archived. No new replies allowed.