### i need help with the calculation part of this program urgently i am new to c++

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263`` ``````#include #include using namespace std; int main() { //variables// string Gender = ""; string Activitylevel = ""; double Weight = 0.0; double Factivecalories = 0.0; double Finactivecalories = 0.0; double Mactivecalories = 0.0; double Minactivecalories = 0.0; int Fmoderatelyactive = 12.0; int Frelativelyinactive = 10.0; int Mmoderatelyactive = 15.0; int Mrelativelyinactive = 13.0; //user information// cout<<"Are you a male or a female?\n"; cin >> Gender; getline(cin, Gender); cout<<"Are you relatively inactive or moderately active? \n"; cin >> Activitylevel; getline(cin, Activitylevel); cout<<"What is your weight?\n"; cin >> Weight; //calculations// if(Gender=="female"){ if(Activitylevel == "moderately acive"){ Factivecalories = Weight * Fmoderatelyactive;} else {Finactivecalories = Weight * Frelativelyinactive;}; } if(Gender == "male"){ if(Activitylevel == "moderately active"){ Mactivecalories = Weight * Mmoderatelyactive ;} else {Minactivecalories = Weight * Mrelativelyinactive;} } //outputs on the screen// if(Gender == "female"){ if(Activitylevel == "moderately acive"){ cout<<"Your total daily calories is:"<< Factivecalories << endl; } else {cout<<"Your total daily calories is:"<
Last edited on
This is the question Please help i got the user info part working but it is not calculating i dont know where i went wrong.

In this exercise, you create a program that displays the number of
daily calories needed to maintain your current weight. Th e number
of calories is based on your gender, activity level, and weight. Th e formulas
for calculating the daily calories are shown in Figure 6-45.

Formulas
Moderately active female: total daily calories = weight multiplied by 12
calories per pound
Relatively inactive female: total daily calories = weight multiplied by 10
calories per pound
Moderately active male: total daily calories = weight multiplied by 15
calories per pound
Relatively inactive male: total daily calories = weight multiplied by 13
calories per pound
Test data Gender Activity Weight
F I 150
F A 120
M I 180
M A 200
Last edited on
Why do you have redundant input request from cin & getline? You can probably get rid of the two getlines.

 ``12345678`` ``````cout<<"Are you a male or a female?\n"; cin >> Gender; getline(cin, Gender); cout<<"Are you relatively inactive or moderately active? \n"; cin >> Activitylevel; getline(cin, Activitylevel); ``````
Thanks but now i need help with the calculations part cant seem to figure that out
I am not able to display a cout cammand i think its my if statements are they ok?
Move the cout below each of the calculations instead of having it at the bottom. I tried it & it works (at least for the first one that I tried).

 ``12345678`` ``````//calculations// if (Gender=="female") { if (Activitylevel == "moderately active") { Factivecalories = Weight * Fmoderatelyactive; cout<<"Your total daily calories is:"<< Factivecalories << endl; }``````

(Cleaned up the appearance.)
Last edited on
I don't see any problems with the if/else statements. You just need to move the cout statements & the calculations should output to the screen.
Thank you so much :)
oh yea one more thing when i build and run the program and when it comes to this part

cout<<"Are you relatively inactive or moderately active? \n";
cin >> Activitylevel;]

When i type relatively inactive or moderately active it skips the weight input part which is

cin >> Weight;

and also skips all the calculations can you help me with that please
Please post your latest program for us to see where the problem(s) might be.
/*oh yea one more thing when i build and run the program and when it comes to this part

cout<<"Are you relatively inactive or moderately active? \n";
cin >> Activitylevel;]

When i type relatively inactive or moderately active it skips the weight input part which is

cin >> Weight;

and also skips all the calculations can you help me with that please
it works only if i TYPE MA or RI but i want to be able to type the whole word
*/

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576`` ``````#include #include using namespace std; int main() { //variables assignment// string Gender = "female"; string Activitylevel = "relaitvely inactive"; double Weight = 0; double Factivecalories = 0; double Finactivecalories = 0; double Mactivecalories = 0; double Minactivecalories = 0; int Femaleactive = 12.0; int Femaleinactive = 10.0; int Maleactive = 15.0; int Maleinactive = 13.0; //user information// cout<<"Are you a male or a female?\n"; //user input for gender// cin >> Gender; cout<<"Are you relatively inactive or moderately active? (Please type RI and MA respectively)\n"; cin >> Activitylevel; cout<<"What is your weight?\n"; cin >> Weight; //outputs on the screen for females// [output] Finactivecalories = Weight * Femaleinactive; //calculation for female inactive calories// if(Gender=="female"){if(Activitylevel=="RI"){cout<<"The number of daily calories needed to maintain your current weight is:" <
Last edited on
I got fed up with using if statements for calculation so i used them for the outputs now the program works but unless i type MA for moderately active or RI for relatively inactive it skips everything after that line. I want to be able to type the whole word.
Last edited on
To type out the words, you will need to replace the "cin >> Activitylevel;" with the "getline(cin, Activitylevel);". The getline will read the white spaces & not cut it off like cin. My mistake in not having you keep the getline.

 ``12`` ``````cout<<"Are you relatively inactive or moderately active? (Please type RI and MA respectively)\n"; cin >> Activitylevel;``````

 ``12`` ``````cout<<"Are you relatively inactive or moderately active? (Please type RI and MA respectively)\n"; getline(cin, Activitylevel);``````

Last edited on
OK but now when i added the "getline(cin, Activitylevel);"
it skips that part and goes straight to weight does it have something to do with the variables?
Change to getline from cin on the first prompt. You will need to update the "RI" with the whole words that you want. I tested it with "female" & used "RI" because it was quick & short.

 ``12`` ``````cout<<"Are you a male or a female?\n"; //user input for gender// cin >> Gender;``````

 ``12`` ``````cout<<"Are you a male or a female?\n"; //user input for gender// Getline(cin, Gender);``````

Last edited on
Thank you so much :)
You're welcome! Glad I was able to help.
Topic archived. No new replies allowed.