Problem with Program

Discussion in 'C++' started by norahs63, Sep 30, 2011.

  1. norahs63

    norahs63 New Member

    Joined:
    Sep 30, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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
     
    Last edited by a moderator: Oct 1, 2011
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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
    
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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 likes this.
  4. norahs63

    norahs63 New Member

    Joined:
    Sep 30, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Appreciate the guidance and help. Indentation has not been covered in class but it's very useful to know! Thanks!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice