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;
}

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.

dang. that worked. I LOVE YOU THANKS FOR BEING NICE ENOUGH TO HELP PEOPLES ON THIS SITE :)

The code you have provided seems to have some issues that might be causing the program to not work as expected. Let's go through it step by step to identify the problem:

1. The variable 'bmi' is used to calculate the doses, but it is not initialized with a value before performing the calculations. This could lead to unpredictable results since the initial value of 'bmi' is unknown.

2. The calculation of 'dose1', 'dose2', 'dose3', and 'dose4' depends on the value of 'bmi', but as mentioned earlier, 'bmi' is uninitialized. To fix this, make sure to assign a valid value to 'bmi' before performing the calculations.

3. Before calculating the dose, you need to check if the 'bmi' entered by the user is valid. It is a good idea to add validation code to ensure that the user enters a reasonable value for 'bmi'. For example, you can check if 'bmi' is greater than zero before proceeding with the calculations.

4. The calculation of 'dose4' seems incorrect because it multiplies 13 with 0.95. To correctly calculate 'dose4', you need to use parentheses to ensure correct order of operations. It should be (bmi * 2) + (13 * 0.95).

5. It looks like the 'if' condition for age less than 12 is placed incorrectly inside the 'gender == 'M'' condition. It should be moved one level up, so that it applies to both genders. Currently, it only applies to males, not females.

Here's an updated version of the code with the mentioned fixes:

```cpp
#include <iostream>
using namespace std;

int main()
{
char gender;
int age, bmi, dose1, dose2;
double dose3, dose4;

cout << "Enter age: ";
cin >> age;
cout << "Enter BMI: ";
cin >> bmi;
cout << "Enter sex (M or F): ";
cin >> gender;

dose1 = bmi * 2;
dose2 = bmi * 2 + 13;
dose3 = bmi * 2 * 0.95;
dose4 = (bmi * 2) + (13 * 0.95);

if (age < 12)
{
if (gender == 'M')
{
cout << "\nDose is: " << dose1 << " units" << endl;
}
else
{
cout << "\nDose is: " << dose3 << " units" << endl;
}
}
else
{
if (gender == 'M')
{
cout << "\nDose is: " << dose2 << " units" << endl;
}
else
{
cout << "\nDose is: " << dose4 << " units" << endl;
}
}

system("PAUSE");
return 0;
}
```

After making these changes, the program should compile and work as expected.