Question
It compiles but it doesn't work. can you tell me what i did wrong?
//Sonny Hinh, variable program, assignment 2
#include <iostream>
using namespace std;
int main ()
{ char gender;
int age, bmi, dose1, dose2;
double dose3, dose4;
dose1 = bmi * 2;
dose2 = bmi * 2 + 13;
dose3 = bmi * 2 * .95;
dose4 = bmi * 2 + 13 * .95;
cout << "Enter age: ";
cin >> age;
cout << "Enter BMI: ";
cin >> bmi;
cout << "Enter sex (M or F): ";
cin >> gender;
if (gender == 'M')
{ if (age < 12)
{ cout << "\nDose is: " << dose1 << " units" << endl;
}
else
{ cout << "\nDose is: " << dose2 << " units" << endl;
}
}
else
{ if (age < 12)
{ cout << "\nDose is: " << dose3 << " units" << endl;
}
else
{ cout << "\nDose is: " << dose4 << " units" << endl;
}
}
system("PAUSE");
return 0;
}
//Sonny Hinh, variable program, assignment 2
#include <iostream>
using namespace std;
int main ()
{ char gender;
int age, bmi, dose1, dose2;
double dose3, dose4;
dose1 = bmi * 2;
dose2 = bmi * 2 + 13;
dose3 = bmi * 2 * .95;
dose4 = bmi * 2 + 13 * .95;
cout << "Enter age: ";
cin >> age;
cout << "Enter BMI: ";
cin >> bmi;
cout << "Enter sex (M or F): ";
cin >> gender;
if (gender == 'M')
{ if (age < 12)
{ cout << "\nDose is: " << dose1 << " units" << endl;
}
else
{ cout << "\nDose is: " << dose2 << " units" << endl;
}
}
else
{ if (age < 12)
{ cout << "\nDose is: " << dose3 << " units" << endl;
}
else
{ cout << "\nDose is: " << dose4 << " units" << endl;
}
}
system("PAUSE");
return 0;
}
Answers
In general, the program "does not work", it can mean a lot of things:
- it does not give any output,
- it does not give the right answer,
- it gives an execution error,
- it reboots the computer
...
Each situation calls for a different solution.
In your particular case, I believe it does not give the right answer.
The computer program executes in sequence. The doses are defined with reference to BMI, when BMI is not input by the user. So in the best of cases, BMI is output as zero (for Java) and probably BMI UNDEF in the case of C++.
All you need to do is to define the different doses AFTER the entry of values by the user, probably just before the "cout" statements.
If you would like them defined at the beginning, you could define them as methods (procedures).
If there are other "symptoms", please specify and post any time.
- it does not give any output,
- it does not give the right answer,
- it gives an execution error,
- it reboots the computer
...
Each situation calls for a different solution.
In your particular case, I believe it does not give the right answer.
The computer program executes in sequence. The doses are defined with reference to BMI, when BMI is not input by the user. So in the best of cases, BMI is output as zero (for Java) and probably BMI UNDEF in the case of C++.
All you need to do is to define the different doses AFTER the entry of values by the user, probably just before the "cout" statements.
If you would like them defined at the beginning, you could define them as methods (procedures).
If there are other "symptoms", please specify and post any time.
dang. that worked. I LOVE YOU THANKS FOR BEING NICE ENOUGH TO HELP PEOPLES ON THIS SITE :)
Related Questions
Can any1 please fix this c++ source problem
/*Circle Are program
Calculates the area of a circ...
Can someone please tell me what I'm doing wrong?
#include <iostream>
#include <iomanip>
using...
when i compile the following, many erors appear
// include necessary libraries
#include <iostr...
Please suggest corrections to this code: why isn't the loop continuing ANY help is appreciated!...