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
Why do you have redundant input request from cin & getline? You can probably get rid of the two getlines.
1 2 3 4 5 6 7 8
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);
#include <iostream>
#include <string>
usingnamespace 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:" <<Finactivecalories<<endl;//output if gender is female and relatively inactive//
}
if (Gender=="female")
Factivecalories = Weight * Femaleactive; //calculations for female active calories//
{ if(Activitylevel!="RI"){ cout<<"The number of daily calories needed to maintain your current weight is:"<<Factivecalories<<endl;//output if gender is female and is not relatively inactive//
}
}
}
[/output]
//outputs on the screen for males//
[output] Minactivecalories = Weight * Maleinactive; //calculations for male inactive calories//
if(Gender!="female"){if(Activitylevel=="RI"){cout<<"The number of daily calories needed to maintain your current weight is:" <<Minactivecalories<<endl;//output if gender is male and relatively inactive//
}
if (Gender!="female")
Mactivecalories = Weight * Maleactive; //calcuulations for male active calories//
{ if(Activitylevel!="RI"){ cout<<"The number of daily calories needed to maintain your current weight is:"<<Mactivecalories<<endl;//output if gender is male and is not relatively inactive//
}
}
}
[/output]
return 0;
}
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.
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.
1 2
cout<<"Are you relatively inactive or moderately active? (Please type RI and MA respectively)\n";
cin >> Activitylevel;
1 2
cout<<"Are you relatively inactive or moderately active? (Please type RI and MA respectively)\n";
getline(cin, Activitylevel);
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.
1 2
cout<<"Are you a male or a female?\n"; //user input for gender//
cin >> Gender;
1 2
cout<<"Are you a male or a female?\n"; //user input for gender//
Getline(cin, Gender);