While loop issues

So this is my first attempt to a while loop and I can't figure it out. The code works just I want it to repeat all of the steps again. All it loops is if i want to repeat (line 40) instead of the entire code. Any help will be helpful.

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
 // Using loops 

#include <cmath>
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;

int main ()
{
	//my variables
	
	float ticket;
	int minpass, maxpass, overhead, number;
	char ans = 'y';
	
	while (ans == 'y')
	{
		while (minpass <= 0 || minpass > 500)
		{
			cout << "please enter the minimum number of passengers ";
			cin >> minpass;
			
			if (minpass <= 0)
			{
				cout << "\nYour minimum passengers cannot be less than or equal to 0";
				cout << "\nPlease enter the minimu number of passengers ";
				cin >> minpass;
			}
			if (minpass > 500)
			{
				cout << "\nMaximum passengers allowed is 500";
				cout << "\nPlease enter Minimum amount of passengers ";
				cin >> minpass;
			}
		cout << "\nMinpass is " << minpass;
		}	 
		
		
		cout << "\nWould you like to run another? (y or n) ";
		cin >> ans;
		system ("cls");
		
	}
return 0;
}
The problem is that minpass keeps its value after the outer loop goes back around, so then the condition while (minpass <= 0 || minpass > 500) becomes false and so the inner loop gets skipped entirely.

What I would do is something closer to
1
2
3
4
5
6
7
8
9
10
11
12
13
while (ans == 'y')
{
    cout << "please enter the minimum number of passengers ";
    cin >> minpass;
    while (minpass <= 0 || minpass > 500)
    {
        // etc.
    }
    cout << "\nMinpass is " << minpass;

    cout << "\nWould you like to run another? (y or n) ";
    cin >> ans;
}
Thanks long double man that worked. So because my condition was before the cin it ignored the condition? sorry for the extra question I just would like to know why it didn't work vs. just giving me the answer.

Thanks in advance.
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?
Check and Check that made perfect sense. Thanks for your help.
Topic archived. No new replies allowed.