Mark44
Mentor
- 38,048
- 10,543
You can make the program a lot simpler by eliminating the switch statement. Also, it simplifies things greatly if you don't confuse the user by asking for numeric and character input (i.e., asking for a temperature followed by the Enter character to quit).yungman said:Yes, I found out after design the program, that switch-case doesn't take conditional statement. That is switch can only take simple case of A, B, C, D etc. It will not read
switch ( temp)
case ( temp <101):
case (temp >103):
I have no choice but to use if-else if - else to put A, B and C for the case statement.
Here's my simplified version.
C++:
// Program checks temperature.
// If 101 <= temp <= 103deg C, check every 15mins.
// If temp > 103 C, turn heat down, check every 5 minutes until correct temp.
// If temp < 101 C, turn heat up, check every 5 minutes until correct temp.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int temp;
do
{
cout << "Enter the temperature, or a negative number to quit. ";
cin >> temp; // Read temperature.
if (temp < 0) break; // If negative, exit do loop.
if (temp < 101)
{
// Temperature too low.
cout << "Raise the temperature, wait 5 minutes and measure again.\n\n";
continue;
}
else if (temp > 103)
{
// Temperature too high.
cout << "Lower the temperature, wait 5 minutes and measure again.\n\n";
continue;
}
else
{
// Temperature is between 101 and 103 degrees C.
cout << "Wait 15 minutes and measure again.\n\n";
continue;
}
} while (true);
cout << "You chose to quit, goodbye.";
}
Input from the user is asked for once, at the top of the loop. The user can enter any integer value 0 or larger, or can enter any negative number to exit the program.