Heya folks. I'm trying to write a BMI calculator, but I'm missing something.

Splint tells me I have a parse error before the scanf, but I'm not seeing it. Also, I'm struggling to get the BMI to print (I'm pretty sure the two are related). It worked (before I put in the ifs), I added the ifs, now it's broken and I can't get it fixed. Certainly it's something simple, but I'm just not seeing it.

Specifically, it enters the while loop with no issue (i.e. when invalid data is entered), but never seems to hit the ifs.

Thanks in advance.

#include <stdio.h>

int
main() 
{
	float height, weight;
		
	printf("Enter your height in inches and weight in pounds: \n");
	scanf("%f%f", &height, &weight);
 	float bmi = (weight/(height*height));	
	
	while(height <= 0 || weight <= 0)
	{
		
		printf("That's not valid data...\n");
		printf("Enter your height and weight: \n");
		scanf("%f%f", &height, &weight);
	}
	
		
        if (bmi < 18.5)
	printf("Your BMI is %.1f.  You're underweight.\n", bmi);
	
	else if (bmi >= 18.5 && bmi <= 24.9)
	printf("Your BMI is %.1f.  You're pretty normal.\n", bmi);
	
	else if (bmi >= 25.0 && bmi <= 29.9)
	printf("Your BMI is %.1f.  You're slightly overweight.\n", bmi);
	
	else 
	printf("Your BMI is %.1f.  You're obese.\n", bmi);}

Edited 7 Years Ago by Happypants: Fixing formatting and clarification

Thanks for responding.

Unfortunately, that doesn't seem to fix the issue. It's not doing the math for bmi when valid data is entered. No matter what data is entered for height and weight the result is always 0.0. (I think I misspoke on the exact problem in my OP) Here's what Splint is spitting out.

bmi.c:11:2: Return value (type int) ignored: scanf("%f%f", &...
Result returned by function call is not used. If this is intended, can cast
result to (void) to eliminate message. (Use -retvalint to inhibit warning)

bmi.c:12:8: Parse Error. (For help on parse errors, see splint -help
parseerrors.)

Those would be lines 9 and 10 for the code posted here.

Due to this error it's always reaching the first if-statement (the result of the equation is always zero).

Edited 7 Years Ago by Happypants: Clarification

gcc doesn't complain, hmm.

it's looks like Splint has a problem.

did you try:

int r = scanf("%f%f", &height, &weight);

float bmi = (float)weight/(height*height);

Are you sure you've got the correct formula?

Checking the return value from scanf is a good idea.

#include <stdio.h>

int main(void)
{
   float height, weight, bmi;
   /*
    * Get user input.
    */
   for ( ;; )
   {
      int ch;
      printf("Enter your height in inches and weight in pounds: ");
      fflush(stdout);
      if ( scanf("%f%f", &height, &weight) == 2 && height > 0 && weight > 0 )
      {
         break;
      }
      while ( (ch = getchar()) != '\n' && ch != EOF ); /* flush input */
   }
   /*
    * Calculate BMI.
    */
   bmi = 703 * (weight / (height * height));
   printf("Your BMI is %.1f.  ", bmi); /* common to all */
   /*
    * Print categorization.
    */
   if ( bmi < 18.5 )
   {
      puts("You're underweight.");
   }
   else if ( bmi <= 24.9 )
   {
      puts("You're pretty normal.");
   }
   else if ( bmi <= 29.9 )
   {
      puts("You're slightly overweight.");
   }
   else
   {
      puts("You're obese.\n");
   }

   return 0;
}

/* my output
Enter your height in inches and weight in pounds: 71.5 f145.6
Enter your height in inches and weight in pounds: 71.5 -145.6
Enter your height in inches and weight in pounds: -71.5 145.6
Enter your height in inches and weight in pounds: 71.5 145.6
Your BMI is 20.0.  You're pretty normal.
*/

Heya folks. I'm trying to write a BMI calculator, but I'm missing something.

Splint tells me I have a parse error before the scanf, but I'm not seeing it.

#include <stdio.h>

int
main() 
{
	float height, weight;
		
	printf("Enter your height in inches and weight in pounds: \n");
	scanf("%f%f", &height, &weight);
 	float bmi = (weight/(height*height));	
	...

You have a variable (bmi) declared not at the top of a block.
This is valid only in a c99 domain; most likely splint assumes an older standard.

man, when i was in the Navy, we had our BMI regularly measured. it used some other measurements like neck and waist but essentially the same.

there was a guy who had a BMI of 8 or 9. many of us had BMIs of 15 or so.

the criteria that <18 is underweight and <15 is starving is bullshit. they've changed the standards to match the average american fatbody .

Are you sure you've got the correct formula?

Checking the return value from scanf is a good idea.

And other stuff you said...

Thanks for the help everybody.

Rewriting the program fixed the problem, Dave... heh. You changed certain parts of it so that now I don't really understand what's going on (i.e. fflush and the "if break" section), but that's for me to figure out now. And I never really thought to check the value of the scanf inside its statement; appreciate

I did have the formula wrong, but at the time it was the least of my worries.

Much appreciated folks. I'm sure there'll be lots more questions where that one came from.

This question has already been answered. Start a new discussion instead.