5 Replies - 478 Views - Last Post: 09 February 2012 - 12:43 AM Rate Topic: -----

#1 cyborg1928  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-February 12

Error with the second if else function

Posted 08 February 2012 - 10:27 PM

I keep getting a syntax error before the last if statement.
It also says
assign03.c:94: error: `Gender' undeclared here (not in a function)
assign03.c:94: warning: data definition has no type or storage class
assign03.c:97: error: syntax error before string constant
assign03.c:97: error: conflicting types for 'printf'



#include <stdio.h>
#include <math.h>
   int main()
   {
      double Inches, Pounds,		//Inputs
             Kilograms, Meters,	//Calculated values
      		 BMI;						//Output
   	char Gender;					//Inputs
   			 
   	//Prompt user for height in inches and weight in pounds
      printf("Enter your height in inches.\n");
      scanf("%lf" , &Inches);
      printf("Enter your weight in pounds.\n");
      scanf("%lf" , &Pounds);
   	
   	
   	/*------------BMI-------------*/	
   	//Compute conversions
      Meters = Inches * .0254;
      Kilograms = Pounds / 2.2046;
   		
   	//Calculate BMI
      BMI = Kilograms / (pow(Meters,2));
   		
   	//Display BMI
      printf("Your BMI is %5.3f lbs.\n", BMI);
      
   	//Display BMI classifications
   	if (BMI < 25)
   			printf("Your BMI is normal!\n");
   	else if (BMI >=25 || BMI < 30)
   			printf("Your BMI is overweight.\n");
   	else if (BMI >=30)
   			printf("Your BMI is obese.\n");
    
      
   	/*--------Target BMI and Weight-------*/
   	//Prompt user for preferred BMI
      printf("Enter your preferred BMI.\n");
      scanf("%lf" , &BMI);
   	
   	//Calculate weight
      Kilograms = BMI * (pow(Meters,2));
   	
   	//Compute conversions
      Pounds = Kilograms * 2.2046;
         
      //Display weight
      printf("You need to weigh %5.3f lbs.\n", Pounds);
   	
   	
   	/*--------IBW---------*/
   	//Prompt user to enter gender (m or f)
   	printf("Please enter your gender by typing m (male) or f (female).\n");
   	scanf(" %c", &Gender)
   	
   	//Compute IBW
   	if (Gender == 'm' || 'M'){
   			Kilograms = 50.0 + 2.3 * (Meters - 60);
   	}
   	else if (Gender == 'f' || 'F'){
   			Kilograms = 45.5 + 2.3 * (Meters - 60);
   	}
   	
   	//Convert IBW
   	Pounds = Gender * 2.2046;
   	
   	//Display IBW
   	printf("Your IBW is %5.3f\n", Pounds);
   	
      return (0);
   	
   }



Is This A Good Question/Topic? 0
  • +

Replies To: Error with the second if else function

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Error with the second if else function

Posted 08 February 2012 - 10:33 PM

You must be trying to compile a different version of your program than what you posted here. The first clue is that your error references lines 94 and 97 that don't exist in this code.

The only syntax error I see in the above is the missing semicolon on line 55. After adding the semicolon it compiles for me without any errors.

edit: PS, in case it wasn't clear from what I wrote above, your "syntax error before the last if statement" is that you're missing a semicolon at the end of line 55.

This post has been edited by r.stiltskin: 08 February 2012 - 10:55 PM

Was This Post Helpful? 1
  • +
  • -

#3 cyborg1928  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-February 12

Re: Error with the second if else function

Posted 08 February 2012 - 10:58 PM

I'm an idiot for not recognizing that.
Thanks A TON!!!
You really saved my ass!
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Error with the second if else function

Posted 08 February 2012 - 11:02 PM

Maybe just too tired.

Glad I could help.
Was This Post Helpful? 0
  • +
  • -

#5 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 550
  • Joined: 12-July 09

Re: Error with the second if else function

Posted 08 February 2012 - 11:48 PM

else if (BMI >=25 || BMI < 30)

That should be &&. With the || only one of the sides have to be true, and the left condition (BMI>=25) is always true if the first if condition (BMI<25) failed.

if (Gender == 'm' || 'M')

That will always be true. The left side (Gender=='m') of the || will evaluate to either true or false, but the right side ('M') will always be true because it doesn't equal 0. You can change it to:
if(Gender == 'm' || Gender == 'M')


Pounds = Gender * 2.2046;

Genders aren't usually involved in multiplication. Shouldn't that be Kilograms?
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Error with the second if else function

Posted 09 February 2012 - 12:43 AM

@Hezekiah: I think it's generally preferable to let people do their own debugging to the extent that they can, & help out where they are stuck.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1