Boolean loop not working correctly

I have written the program below and it runs successfully but when the menu choice is input, it just reruns the loop. How can I correct this?


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>
#include<iomanip>
using namespace std;

const char QUIT = '0';                  // constants for menu options
const char INPUT = '1';
const char PROCESS = '2';
const char OUTPUT = '3';

int main()
{
	char userSelection;                 // input for menu choice
	bool done;                          // flag for loop control



	// TODO: Begin loop here.  Test the loop control variable
	// Do NOT test the userSelection variable
	while (!done) {

		
		cout << endl
			<< setw(25) << "    Program Menu     " << endl
			<< setw(25) << "---------------------" << endl
			<< setw(25) << "1 ...... Input       " << endl
			<< setw(25) << "2 ...... Process     " << endl
			<< setw(25) << "3 ...... Output      " << endl
			<< setw(25) << "0 ...... Quit Program" << endl
			<< setw(25) << "---------------------" << endl;



		cout << setw(20) << "Enter option: ";
		cin >> userSelection;

        }
	if (userSelection == 0)
	{
		done = true;
		cout << "Invalid";

	}
	else if	(userSelection == 1)
	{
		cout << "Do INPUT stuff";
	}
	else if (userSelection == 2)
	{
		cout << "Do PROCESS stuff";
	}
	else if (userSelection == 3)
	{
		cout << "Do OUTPUT stuff";
	}
	else
	{
		cout << "Invalid choice, please try again.";
	}
		// TODO: End loop here.


		cout << "\nEnd Program - ";

} 


/* Sample Program Interaction and Output

Program Menu
---------------------
1 ...... Input
2 ...... Process
3 ...... Output
0 ...... Quit Program
---------------------
Enter option: 1

Do INPUT stuff

Program Menu
---------------------
1 ...... Input
2 ...... Process
3 ...... Output
0 ...... Quit Program
---------------------
Enter option: 2

Do PROCESS stuff

Last edited on
Please add code tags to your post. See http://www.cplusplus.com/articles/jEywvCM9/
Set systematic indentation. If you do, you should see your logical error.
your while loop ends on line 36. this is one of the dangers of using book-brackets instead of having brackets on a line by themselves.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while(!done)
{
    if(cond)
     {
        code();
     }
}//aligns to while start bracket.


vs

while(!done){
    if(cond){
     code();
     } //what does this belong to?  Can't see it visually. 
}  //or this, where did it start? nothing in the same column


that said book-indent is a thing people use, but it has no merits apart from saving money when printing a physical book.

Thanks, that helped. I also should have been using the declared constants in the if else statements instead of numbers. For example,
1
2
3
4
if (userSelection == INPUT)
{
     cout << "Do INPUT stuff";
}





Last edited on
look up enum… you are re-creating the idea here.
1
2
3
4
5
char userSelection;
const char PROCESS = '2';

if (userSelection == INPUT) // OK, comparing char to char
if (userSelection == 2) // 2 != '2'. The 2 is ASCII code for one of the unprintable chars? 


Alternative to enums:
1
2
3
unsigned int userSelection;
std::string replies[] {"Invalid", "input", "process"};
std::cout << "Do " << replies[userSelection];

(That obviously requires sanity checks.)

One could put function pointers rather than strings into that array ...
Topic archived. No new replies allowed.