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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include<string>

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:"<<Finactivecalories <<endl;}
                                 }

                                      if(Gender == "male"){ if(Activitylevel == "moderately acive"){
               cout<<"Your total daily calories is:"<< Mactivecalories << endl;
                                 }
                                 else {cout<<"Your total daily calories is:"<<Minactivecalories <<endl;}
                                 }




return 0;
}
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.


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);
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).

1
2
3
4
5
6
7
8
//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

cout<<"What is your weight?\n";
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

cout<<"What is your weight?\n";
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
*/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <string>

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:" <<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;
}
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.


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

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.

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

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