While loop question

It appears that my while() loop doesn't understand characters ('a') only integers. Even when I tried to store input as a char, the while loop would never exit. To complete this assignment as instructed I need to use letters and numbers in my menu.

So is it true that while loops only understand integers? Also any ideas on how I might solve this dilemma?

Thanks.

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
63
64
 #include <iostream>

using namespace std;

int main()
{
	int input = 0;

	//Menu
	while (input != 'q' || input != 'Q')
	{

		cout << "s - report the machine status\n";
		cout << "d - drop in a quarter\n";
		cout << "1 - pull the 1st knob\n";
		cout << "2 - pull the 2nd knob\n";
		cout << "3 - pull the 3rd knob\n";
		cout << "4 - pull the 4th knob\n";
		cout << "r - restock the machine\n";
		cout << "q - quit\n";
		cin >> input;

		switch (input){
			case 's':
			case 'S':
				//report the machine status
				break;
			case 'd':
			case 'D':
				//drop in a quarter
				break;
			case 1:
				//pull the first knob
				break;
			case 2:
				//pull the second knob
				break;
			case 3:
				//pull the third knob
				break;
			case 4:
				//pull the fourth knob
				break;
			case 'r':
			case 'R':
				//restock the machine
				break;
			case 'q':
			case 'Q':
				//quit
				cout << input << "\n";
				break;
			default:
				cout << "I do not understand\n";
		}
		
	}
			
	//Outside while loop
	


	return 0;
}
Input isn't of char type.

Furthermore, you have to declare it to something first(or take input into it). Otherwise it contains trash data.

And if you check for some data(compare it), be consistent:

if input is of type char, check for '1' rather than for 1.

And answering your answer: while loop is basing on bool value, so it can vary from simple bool, through operator== to some more complex functions.

And yeah, premature answer. Daleth pointed what I wanted to say: you must change OR to AND. Otherwise it will not work.
Last edited on
Let's see what happens if you enter 'q'.
While condition:
Is 'q' != 'q' ? False, check next condition
Is 'q' != 'Q' ? True, run another loop.

Change the OR to AND.
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
63
64
#include <iostream>

using namespace std;

int main()
{
	char input;

	//Menu
	while (input != 'q' && input != 'Q')
	{

		cout << "s - report the machine status\n";
		cout << "d - drop in a quarter\n";
		cout << "1 - pull the 1st knob\n";
		cout << "2 - pull the 2nd knob\n";
		cout << "3 - pull the 3rd knob\n";
		cout << "4 - pull the 4th knob\n";
		cout << "r - restock the machine\n";
		cout << "q - quit\n";
		cin >> input;

		switch (input){
			case 's':
			case 'S':
				//report the machine status
				break;
			case 'd':
			case 'D':
				//drop in a quarter
				break;
			case '1':
				//pull the first knob
				break;
			case '2':
				//pull the second knob
				break;
			case '3':
				//pull the third knob
				break;
			case '4':
				//pull the fourth knob
				break;
			case 'r':
			case 'R':
				//restock the machine
				break;
			case 'q':
			case 'Q':
				//quit
				cout << input << "\n";
				break;
			default:
				cout << "I do not understand\n";
		}

	}

	//Outside while loop



	return 0;
}
Topic archived. No new replies allowed.