Let's run through your program.
13 14 15
|
float ticket;
int minpass, maxpass, overhead, number;
char ans = 'y';
|
Okay, so we declare a bunch of variables. Most of them don't actually get used, but I assume you'll be adding more stuff to this program later.
Also note that
minpass is uninitialized at this point, so it could really have just about any value right now.
17 18
|
while (ans == 'y')
{
|
We see that
ans does indeed equal
'y'
, so we enter the loop.
19 20
|
while (minpass <= 0 || minpass > 500)
{
|
Okay,
minpass is still uninitialized, so there's really no way to tell if this condition is true or false.
Apparently, you got lucky and it ended up being true, so we enter the loop. (Don't count on it next time, though.)
21 22 23
|
cout << "please enter the minimum number of passengers ";
cin >> minpass;
// etc. (checking for <= 0 or > 500)
|
Let's say you enter 300. The next two
if
checks come out
false
, so none of that is executed.
36 37
|
cout << "\nMinpass is " << minpass;
}
|
This prints
Minpass is 300 (as expected). Now, this is the end of the
while (minpass <= 0 || minpass > 500)
loop, so we go back up to the top of the loop:
19 20
|
while (minpass <= 0 || minpass > 500)
{
|
Since
minpass is 300 at this point, this condition is false and the loop is not entered.
40 41 42 43 44
|
cout << "\nWould you like to run another? (y or n) ";
cin >> ans;
system ("cls");
}
|
Let's say we enter
'y'
. The
while (ans == 'y')
loop ends here, so we go back up to the top of the loop.
17 18
|
while (ans == 'y')
{
|
Since
ans is
'y'
, we enter the loop.
19 20
|
while (minpass <= 0 || minpass > 500)
{
|
minpass is still 300. This condition is false, so the loop body is not executed.
40 41 42 43 44
|
cout << "\nWould you like to run another? (y or n) ";
cin >> ans;
system ("cls");
}
|
Oh hey, we're back here! See what went wrong?