Basic While 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
	while(loop = 1) // Continues to prompt if amount was inputted incorrectly.
		{
			switch (class1_days)
				{
					case 1:
						loop = 0;
						break;
					case 2:
						loop = 0;
						break;
					case 3:
						loop = 0;
						break;
					case 4:
						loop = 0;
						break;
					case 5:
						loop = 0;
						break;
					default:
						cout << "The amount you entered is not operable, try again: ";
						cin >> class1_days;
						break;
				}
		}


When I run this short bit of code in my script it fails to do anything if any of the cases are chosen, although it does execute the default if one of the cases is not chosen.
Hard to tell everything you want to do from that code piece, however your while loop should be while(loop == 1) not while(loop = 1)

= is for assigning directly to variables, == is to compare 2 variables.
That old chestnut. Commence kicking yourself. :-)

while(loop = 1)

...is different to...

while(loop == 1)

Last edited on
Hi Scoobs,

That's because the case statements don't output anything.

1
2
loop = 0;
break;


all that does is set loop to 0

if you had

1
2
3
4
case 2:
	loop = 0;
        cout << "This is the second case << endl;
	break; 


That would produce some output.


Edit: My bad for not noticing this , however what i said what still right
while(loop = 1)
Last edited on
*Facepalm* Thanks guys.
TheIdeasMan, the while loop was in place to just continue prompting until a correct number was inputted, once a correct number was inputted, it could go on with the rest of the code. (Hence the loop=0)
A better way is to use a bool say GoodInput and test that in the while expression, set it in the default clause of the switch.

However this is no good if it is C language.
Topic archived. No new replies allowed.