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

Sep 30, 2012 at 1:56am
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 Sep 30, 2012 at 5:39pm
Sep 30, 2012 at 1:57am
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 Sep 30, 2012 at 2:01am
Sep 30, 2012 at 3:28am
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);
Sep 30, 2012 at 3:38am
Thanks but now i need help with the calculations part cant seem to figure that out
Sep 30, 2012 at 3:53am
I am not able to display a cout cammand i think its my if statements are they ok?
Sep 30, 2012 at 3:58am
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 Sep 30, 2012 at 4:20am
Sep 30, 2012 at 4:28am
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.
Sep 30, 2012 at 11:38am
Thank you so much :)
Sep 30, 2012 at 1:07pm
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
Sep 30, 2012 at 4:35pm
Please post your latest program for us to see where the problem(s) might be.
Sep 30, 2012 at 5:04pm
/*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 Sep 30, 2012 at 6:11pm
Sep 30, 2012 at 5:06pm
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 Sep 30, 2012 at 5:07pm
Sep 30, 2012 at 5:27pm
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 Sep 30, 2012 at 5:27pm
Sep 30, 2012 at 5:36pm
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?
Sep 30, 2012 at 6:50pm
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 Sep 30, 2012 at 6:53pm
Sep 30, 2012 at 9:30pm
Thank you so much :)
Sep 30, 2012 at 10:20pm
You're welcome! Glad I was able to help.
Topic archived. No new replies allowed.