If statement is not being selected when condition is met

Hi, I am writing a console application that gives the user a list of options to choose from. Viewing the code below, when the option "0 - Exit" is selected my code does not run the if statement for when "0" is entered and the loop is infinite. Can anyone please advise me as to why this is?


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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  while (true)
	{
		std::cout << "\nPlease Make A Selection:" << std::endl;

		std::cout << "1 - Add Patient" << std::endl;
		std::cout << "2 - Process Next Patient In Queue" << std::endl;
		std::cout << "3 - Display Queue" << std::endl;
		std::cout << "4 - View Processed Patients History" << std::endl;
		std::cout << "5 - Load Queue" << std::endl;
		std::cout << "6 - Save Queue" << std::endl;
		std::cout << "0 - Exit" << std::endl;
		std::cout << "\nSelection: ";
		std::cin >> user_selection;

		flush_buffer();

		while (user_selection)
		{
			if(user_selection == 0)
			{
				break;
			}

			if (user_selection == 1)
			{
				std::cout << "Enter patient name: ";
				std::getline(std::cin, name);

				do
				{
					std::cout << "\tEnter ailment (leave blank when done): ";
					std::getline(std::cin, ailment);
					if (ailment.empty())
					{
						break;
					}

					std::cout << "\tEnter severity: ";
					std::cin >> severity;

					std::cout << "\tEnter time criticality: ";
					std::cin >> time_criticality;

					std::cout << "\tEnter contagiousness: ";
					std::cin >> contagiousness;

					flush_buffer();


				} while (!ailment.empty());

				// push_back to linkedlist? patient? ailment?

				break;
			}

			if(user_selection == 2)
			{
				std::cout << " moved to patient room!" << std::endl;
				// Process patient with highest priority
				break;
			}

			if(user_selection == 3)
			{
				std::cout << "Patients In Queue:" << std::endl;
				// Display patient queue
				break;
			}

			if(user_selection == 4)
			{
				std::cout << "History:" << std::endl;
				// Display history of processed patients
				break;
			}

			if(user_selection == 5)
			{
				std::cout << "Enter path to file: ";
				// Load a .csv file
				break;
			}

			if (user_selection == 6)
			{
				std::cout << "Enter path to file: ";
				// Save to .csv file
				break;
			}

			if(user_selection == 0)
			{
				EXIT_SUCCESS;
			}
		}
	}
}
Line 17 is while (user_selection).

A conditional statement will be considered true if it converts to anything other than zero.

So Line 17 is equivalently, "while (user_selection != 0)". user_selection is 0, so the inner while loop will not be entered.

Even if the inner while loop were to be entered, your if statement on line 19 would cause the inner while loop to break, so your if statement on line 92 would never be reached.

And even if the if statement on line 92 were entered,
1
2
3
4
			if (user_selection == 0)
			{
				EXIT_SUCCESS;
			}

The above doesn't do anything.
EXIT_SUCCESS is just a macro for 0, so you're just writing:
1
2
3
4
if (user_selection == 0)
{
    0; // no-op
}

(I suppose you meant to return EXIT_SUCCESS.)
Last edited on
Topic archived. No new replies allowed.