Nested if program
Jan 30, 2014 at 4:44am UTC
I am a beginning programmer and I've written this program to give 1 of 4 diagnoses based on the answers given.
Two questions:
1) Is there a more efficient way I could code this?
2) The program does not work properly if I enter:
y
y
y
y
98.6
The result should be to go see a specialist but the program is telling me it's a cold. It should not be a cold since there's achiness and a headache. What is the potential problem?
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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const double VISIT = 40.00,
PRESCRIPTION = 15.00;
char runnyNose,
nasalCongestion,
achy,
headache;
double temperature;
cout << setprecision(2) << fixed << showpoint;
cout << endl << "Do you have a runny nose? (Enter 'y' or 'n') " ; //obtain information from patient
cin >> runnyNose;
cout << endl << "Are you experiencing nasal congestion? (Enter 'y' or 'n') " ;
cin >> nasalCongestion;
cout << endl << "Are you feeling achy all over? (Enter 'y' or 'n') " ;
cin >> achy;
cout << endl << "Do you have a severe headache behind one eye? (Enter 'y' or 'n') " ;
cin >> headache;
cout << endl << "What is you temperature? " ;
cin >> temperature;
if ((runnyNose == 'y' ) && (nasalCongestion == 'y' ) && (temperature < 99.0))
{
cout << endl << "You have a cold.\n"
"Rest and drink plenty of fluids.\n"
"Your bill is $" << VISIT << ".\n" ;
}
else if ((runnyNose == 'y' ) && (nasalCongestion == 'y' ) && (achy == 'y' ) && (temperature >= 99.0) && (temperature < 101.0))
{
cout << endl << "You have respiratory influenza.\n"
"Rest, drink plenty of fluids, and take Tylenol or aspirin.\n"
"Your bill is $" << VISIT << ".\n" ;
}
else if ((runnyNose == 'y' ) && (nasalCongestion == 'y' ) && (achy == 'y' ) && (headache == 'y' ) && (temperature >= 101.0))
{
cout << endl << "You have sinusitis.\n"
"Rest, drink plenty of fluids, take Tylenol or aspirin, and take.\n"
"250mg of penicillin 4 times a day.\n"
"Your bill is $" << VISIT + PRESCRIPTION << ".\n" ;
}
else
cout << endl << "Sorry, you need to see a specialist.\n"
"Your bill is $" << VISIT << ".\n" ;
system("pause" );
return 0;
}
Last edited on Jan 30, 2014 at 4:45am UTC
Jan 30, 2014 at 4:48am UTC
You need to add conditions that the third and fourth answers must be 'n', otherwise it doesn't matter what they are when considering the requirements to be diagnosed with a cold.
Topic archived. No new replies allowed.