Post a New Question

C++ if else problem, please look

posted by on .

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

  • C++ if else problem, please look - ,

    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.

  • C++ if else problem, please look - ,

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

Answer This Question

First Name:
School Subject:
Answer:

Related Questions

More Related Questions

Post a New Question