Could you please tell me what is wrong with my code?
I recently learned how to use if - else statements and I'm getting an error here like crazy.
Building weighthight.obj.
C:\Users\Deimler\Desktop\C Programing\New folder\weighthight.c(20): error #2001: Syntax error: expected ')' but found 'integer constant'.
C:\Users\Deimler\Desktop\C Programing\New folder\weighthight.c(20): error #2001: Syntax error: expected ';' but found ')'.
C:\Users\Deimler\Desktop\C Programing\New folder\weighthight.c(20): error #2061: Illegal statement termination.
C:\Users\Deimler\Desktop\C Programing\New folder\weighthight.c(23): error #2157: Unrecognized statement.
C:\Users\Deimler\Desktop\C Programing\New folder\weighthight.c(23): error #2001: Syntax error: expected ';' but found 'if'.
C:\Users\Deimler\Desktop\C Programing\New folder\weighthight.c(26): error #2039: Illegal expression.
C:\Users\Deimler\Desktop\C Programing\New folder\weighthight.c(26): error #2088: Lvalue required.
C:\Users\Deimler\Desktop\C Programing\New folder\weighthight.c(26): error #2001: Syntax error: expected ')' but found 'floating constant'.
C:\Users\Deimler\Desktop\C Programing\New folder\weighthight.c(26): warning #2030: = used in a conditional expression.
C:\Users\Deimler\Desktop\C Programing\New folder\weighthight.c(26): error #2001: Syntax error: expected ';' but found ')'.
C:\Users\Deimler\Desktop\C Programing\New folder\weighthight.c(26): error #2061: Illegal statement termination.
C:\Users\Deimler\Desktop\C Programing\New folder\weighthight.c(29): error #2157: Unrecognized statement.
C:\Users\Deimler\Desktop\C Programing\New folder\weighthight.c(29): error #2001: Syntax error: expected ';' but found '('.
C:\Users\Deimler\Desktop\C Programing\New folder\weighthight.c(30): error #2001: Syntax error: expected ';' but found 'printf'.
*** Error code: 1 ***
Done.
Here's my code.
#include <stdio.h>
#include <math.h>
int
main()
{
double bmi;
double h;
double w;
printf("What is your height in Inches -> ");
scanf("%lf", &h);
printf("What is your weight in lb -> ");
scanf("%lf", &w);
bmi = (703 * w) / (h * h);
printf( "The bmi is %f ", bmi);
if (bmi <= 18. 5)
printf ("Your BMI is %f you are -> Underweight\n", bmi);
else if (18.5 <= bmi <= 24.9)
printf ("Your BMI is %f you are -> Normal\n", bmi);
else if (25.0 <=bmi=<29.9)
printf ("Your BMI is %f you are -> Overweight\n", bmi);
else (bmi >= 30.0)
printf (" Your BMI is %f you are -> Obese\n", bmi);
return 0;
}
Hope you can help, its driving me crazy.
Three simple syntax errors.
#include <stdio.h>
#include <math.h>
int main()
{
double bmi;
double h;
double w;
printf("What is your height in Inches -> ");
scanf("%lf", &h);
printf("What is your weight in lb -> ");
scanf("%lf", &w);
bmi = (703 * w) / (h * h);
printf( "The bmi is %f ", bmi);
if (bmi <= 18.5) //Was: if (bmi <= 18. 5)
printf ("Your BMI is %f you are -> Underweight\n", bmi);
else if (18.5 <= bmi <= 24.9)
printf ("Your BMI is %f you are -> Normal\n", bmi);
else if (25.0 <= bmi && bmi <= 29.9) //Was: else if (25.0 <=bmi=<29.9)
printf ("Your BMI is %f you are -> Overweight\n", bmi);
else //Was: else (bmi >= 30.0)
printf (" Your BMI is %f you are -> Obese\n", bmi);
return 0;
}
Debugging tips:
If you use the IDE you can double click the first error and the cursor will jump to it in the source code. Next look at the line and ask yourself "What could be causing this here?" Fix the code and attempt to recompile. The list of errors will be greatly reduced.
Repeat the process untill you have fixed them all.
Regards,
DMAC
Quote from: Deimler on January 18, 2011, 05:14:18 PM
Could you please tell me what is wrong with my code?
I recently learned how to use if - else statements and I'm getting an error here like crazy.
Some very simple errors:
1) take a close look at this line ;)
Quoteif (bmi <= 18. 5)
Notice something that shouldn't be there? ;)
2)and those two lines ain't working in C (nor in any other programming language AFAIK)
Quoteelse if (18.5 <= bmi <= 24.9)
...
else if (25.0 <=bmi=<29.9)
those should be in the form of
Quoteif ((25.0 <= bmi) && (bmi <= 24.9))
where you also should note that =< is not a valid construction either...
Ralf
Apart from the previous errors, one more thing i noted: you used %f instead of %lf as you should have done. dont know if that is anyway related to the error but maybe that can make your code better.