Please review my code

Im very new to c++, and would like you all to review the source code I wrote for a program that calculates the users BMI and loops the program back to the start if the user needs to do additional calculations. Thanks =]



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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <stdlib.h>
using namespace std;

float bmiNumber(float pounds, float inches)                                                                                       //BMI calculation
{
      return ((pounds/inches)*703);
      }

void chart(int mF)
{
     if(mF==2) {
     cout<<"\nHere is some info you might find useful...\n"
         <<"At the same BMI, women tend to have more body fat than men."<<endl
         <<"From cdc.gov\n"<<endl;
         }
}
void history()
{
     cout<<"Version History"<<endl;
     cout<<"\tVersion 1.0.1(05/18/2012)"<<endl
         <<"Optimized source code for better program flow,\nImproved calculation methods,\n"
         <<"Introduded looping capabilities for\nmultiple calculations without restarting the program."<<endl;
     cout<<"\tVersion 1.0.0(05/15/2012)"<<endl
         <<"Creation of the program, very rough outline of the basic structure."<<endl;
         system ("pause");
}
     

int main()
{
int changes;
int m;
m=1;

	cout<<"Hello and welcome to the BMI Calculation Tool 1.0.1, or BMICT1.0.1 for short."<<endl;
	cout<<"If you would like to see the changes from the previous version(1.0.0)\nplease press 3..."<<endl;
	cout<<"If not press any other key.";
     cin>>changes;
     if(changes==3)
     {
                   history();
                   }
    cout<<endl<<endl<<endl;               
	cout<<"Why don't you first tell me a little bit about yourself,\nby answering just one simple question."<<endl;
	cout<<"Are you a man or a woman?";
     
     
    while(m==1)
    { 
    cout<<"If you are a man, please press 1,\nif you are a woman, please press 2."<<endl;
	cout<<"Please make your selection now..."<<endl;
     cin>>m;

    
    
     if(m==1)
     {
            cout<<"Our sources have indicated that you are in fact a man,\n" 
                <<"please check to confirm this and continue with the program"<<endl;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
                }
     else if(m==2)
     {
         cout<<"Our sources have indicated that you are a woman,\n"
             <<"if this is true please continue with the program"<<endl;
}

      else
      {
          cout<<"You have entered an incorrect key..."<<endl;
          cout<<"The program will now close.\n";
         
          system ("pause");
          return 0;
}
          chart(m);
 
      float feet;
      float inch; 
      float totalinches;          
      cout<<"Please enter your height as follows..."<<endl;                 
      cout<<"Number of feet: "<<endl;
      cin>>feet;
      
      cout<<"\nNumber of inches: "<<endl;
      cin>>inch;
      
      totalinches=(feet*12)+inch;
      
     cout<<"Your total height in inches is: "<<totalinches<<endl;  
     float inchSqd;
     float lbs;
     float BMI;
     inchSqd=(totalinches*totalinches);
     cout<<"Please enter your weight in pounds: \n";
     cin>>lbs;
     cout<<"Okay, we will now calculate your BMI\n";            
     BMI=bmiNumber(lbs, inchSqd);                                    
      cout<<endl
      <<"Your Body Mass Index Number is: "<<BMI<<endl;
  
     cout<<endl<<"Once again your BMI is "<<BMI<<", and here is a chart for review."<<endl;
     cout<<endl
         <<    	"\tWeight Status"<<endl
         <<" Below 18.5 	Underweight"<<endl
         <<" 18.5 to 24.9 	Normal"<<endl
         <<" 25.0 to 29.9 	Overweight"<<endl
         <<" 30.0 and Above Obese"<<endl;
     cout<<"If you would like to make another calculation press 1, if not press 2..."<<endl;
     cin>>m;
     }
     
     cout<<endl<<"Thank you for using the BMICT1.0.1 and have a great day.\n";

  system ("pause"); 
	return 0;
	}
Line 60: Did you get bored and kept Space pressed just to see how far it goes?

#include <stdlib.h> should be #include <cstdlib> for modern C.

Your code looks a bit "all over the place", because of indentation. (Curly braces should be vertically aligned in your case.) Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void function()
{
    if (...)
    {
        int a; // <-- notice the levels
        float b;

        while (...)
        {
            {
            }
        }
    }
} // <-- notice alignment with the opening brace 


A few extra spaces wouldn't hurt either:
cout << endl << "Thank you for using the BMICT1.0.1 and have a great day.\n"; if (changes == 3)

Other things are minor, and you'll learn them yourself as you become a better C++ programmer.
As far as line 60 goes I prefer the insertion/extraction operators to be aligned, though I suppose you might mean how the box is so wide, I have no idea how that happened. And you make a good point about the curly braces, that's a lot more readable. Thanks for the review catfish =] I'll apply these things in my next program.
You don't do any error checking.
If the user enters something that is not a number, your program will still continue.

__________________________________________
Visit my project: http://www.derivative-calculator.net
another good point, though I'm not particularly sure how to check for characters, id imagine an if statement but idk the parameters id use.
Why is there endl all over the place? While (on most systems) it doesn't hurt much with cout, it will be a major slowdown if you decide to send this output to a file. Use \n, as you do in many other places.

system("pause"); is pretty much the worst way to "keep the console window from closing". There's a stick post about it, I would just not use it at all (ctrl-f5 to run from visual studio, or open console window and run there)

What is m doing all the way in the beginning of main()?

Why totalinches and BMI and inchSqd are created with no sensible values?

And yes, input needs to be checked.

here's a start:

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
#include <iostream>
#include <string>
using namespace std;

//BMI calculation
float bmiNumber(float pounds, float inches) 
{
    return pounds/inches*703;
}

void chart(int mF)
{
    if(mF==2)
       cout << "\nHere is some info you might find useful...\n"
               "At the same BMI, women tend to have more body fat than men.\n"
               "From cdc.gov\n\n";
}

void history()
{
     cout << "Version Histor\n"
             "\tVersion 1.0.1(05/18/2012)\n"
             "Optimized source code for better program flow,\n"
             "Improved calculation methods,\n"
             "Introduded looping capabilities for\n"
             "multiple calculations without restarting the program.\n"
             "\tVersion 1.0.0(05/15/2012)\n"
             "Creation of the program, very rough outline of the basic structure.\n";
}
     

int main()
{
    cout << "Hello and welcome to the BMI Calculation Tool 1.0.1, or BMICT1.0.1 for short.\n"
            "If you would like to see the changes from the previous version(1.0.0)\n"
            "please press 3...\n"
            "If not press any other key.";
    int changes;
    cin >> changes;
    if(!cin) // non-integer was entered
    {
        cin.clear(); // clear error
        cin.ignore(1000, '\n'); // chomp the line
    } else if(changes == 3)
        history();
    cout << "\n\n\n"
              "Why don't you first tell me a little bit about yourself,\n"
          "by answering just one simple question.\n"
              "Are you a man or a woman?";
     
    cout << "If you are a man, please press 1,\n"
            "if you are a woman, please press 2.\n"
            "Please make your selection now...\n";
    int m;
    cin >> m;
    if(!cin || m < 1 || m > 2)
    {
        cout << "You have entered an incorrect key...\n"
                "The program will now close.\n";
        return 1;
    }

    string gender[3] = {"", "man", "woman"};
    cout << "Our sources have indicated that you are in fact a " << gender[m] << '\n'
         << "please check to confirm this and continue with the program\n";

    chart(m);
 
    cout << "Please enter your height as follows...\n"
            "Number of feet: \n";
    // etc.. remember to check after any cin
}
Last edited on
Topic archived. No new replies allowed.