Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Problem with Program (http://www.go4expert.com/forums/problem-with-program-t26823/)

norahs63 1Oct2011 01:49

Problem with Program
 
I am incredibly new to c++. I am just stumped. I've been spending hours fixing and compiling the code below and I keep getting errors. It's a BMI calculator. Enter height and weight and it calculates and shows BMI and then depending on what your BMI is returns a message. I think I'm putting the { in the wrong places although I've gone over the instructions and googled like crazy for ideas. If someone can see what I'm doing wrong, I'd appreciate the feedback! Thanks!

Code:

#include <iostream>
#include <iomanip>
using namespace std;

    int main()

{
    //declare variables
    float weight = 0;
    float height = 0;
    float BMI    = 0;
         
    //enter height and weight
    cout << "Height?  ";
    cin >> height;
    cout << "Weight?  ";
    cin >> weight;

    //calculate BMI
    BMI = 703*weight/(height*height);

    cout << "Your BMI is " << BMI << endl;
   
   
if (BMI <= 18.5 )
{
    cout<<"You are underweight\n";
}
else
{                                          //1

    if (BMI > 19 && BMI <=24)
        {
          cout<<"Your weight is normal\n"; 
        }
      else
        {                                //2
          if (BMI > 25 && BMI <=29)
            {
                cout<<"You are overweight\n";
            }
            else
            {                            //3
                if (BMI => 30)
                {
                    cout<<"You are obese\n";
                  }
                }                            //end of 4
              }                              // end of 3
            }                              // end of 2
            }                              //end of 1
// end of else


      system("pause");
      return 0;

}    //end of main function


xpi0t0s 2Oct2011 13:18

Re: Problem with Program
 
The most obvious thing that is wrong is your use of indentation. Learn to use this correctly and any errors like this will be blindingly obvious. This is exactly the same code, but indented correctly: at a {, indent one tab more, and at each } indent by one tab less, in such a way as for the { and } to line up. For example:
Code:

int main()
{ // Top level brace is in column 1
    // First line of code is one tab (or 4 spaces as in this post) in
    if (some_test)
    { // This brace is in the same column as the if
        cout << "Test is true" << endl; // This code is another 4 spaces in
    } // This brace lines up with its opening brace
} // This brace lines up with its opening brace
} // This brace is a syntax error, and is immediately obvious because we cannot lose 4 more spaces.

So here's your code, and you can scan down for the problem:
Code:

#include <iostream>
#include <iomanip>
using namespace std;

int main()

{
        //declare variables
        float weight = 0;
        float height = 0;
        float BMI    = 0;
         
        //enter height and weight
        cout << "Height?  ";
        cin >> height;
        cout << "Weight?  ";
        cin >> weight;

        //calculate BMI
        BMI = 703*weight/(height*height);

        cout << "Your BMI is " << BMI << endl;
   
   
        if (BMI <= 18.5 )
        {
                cout<<"You are underweight\n";
        }
        else
        {                                          //1

                if (BMI > 19 && BMI <=24)
                {
                        cout<<"Your weight is normal\n"; 
                }
                else
                {                                //2
                        if (BMI > 25 && BMI <=29)
                        {
                                cout<<"You are overweight\n";
                        }
                        else
                        {                            //3
                                if (BMI => 30)
                                {
                                        cout<<"You are obese\n";
                                }
                        }                            //end of 4
                }                              // end of 3
        }                              // end of 2
}                              //end of 1
// end of else


system("pause");
return 0;

}    //end of main function


xpi0t0s 2Oct2011 13:24

Re: Problem with Program
 
By the way you can also use an if - else if ladder; you do not have to indent one level each time, and this is perfectly readable:
Code:

        if (BMI <= 18.5 )
        {
                cout<<"You are underweight\n";
        }
        else if (BMI > 19 && BMI <=24)
        {
                cout<<"Your weight is normal\n"; 
        }
        else if (BMI > 25 && BMI <=29)
        {
                cout<<"You are overweight\n";
        }
        else if (BMI => 30)
        {
                cout<<"You are obese\n";
        }

=> is not a valid operator; you mean >=. Or you could just miss off "if (BMI => 30)" altogether and just end with an else block, as in:

Code:

        if (BMI <= 18.5 )
        {
                cout<<"You are underweight\n";
        }
        else if (BMI > 19 && BMI <=24)
        {
                cout<<"Your weight is normal\n"; 
        }
        else if (BMI > 25 && BMI <=29)
        {
                cout<<"You are overweight\n";
        }
        else
        {
                // you won't be in here if BMI is less than 30, so an explicit test is unnecessary.
                cout<<"You are obese\n";
        }


norahs63 3Oct2011 19:46

Re: Problem with Program
 
Appreciate the guidance and help. Indentation has not been covered in class but it's very useful to know! Thanks!


All times are GMT +5.5. The time now is 15:47.