1

I am practicing c programming now and I made some BMI calculator. It works great but when user input string or character on the first scanf that send data to float variable 'weight' . It passed all scanf after that and showed error to me that Your BMI is 1.#J . How to solve this error? Here is my code.

#include <stdio.h>

int main()
{
  float weight;
  float height;
  float result;
  float c_height;

  printf("Enter your weight as kilogram...\n\n> ");
  scanf("%f",&weight);
  //If user input char or string , it passed all thing and showed error.

  printf("\nEnter your height as centimetres...\n\n> ");
  scanf("%f",&height);

  c_height=(height/100)*(height/100);
  result=weight/c_height;

  if(result<18.50)
  {
    printf("\nYour BMI is %.2f\n\n",result);
    printf("You are underweight! Try to eat more frequently\n\n");
    printf("Thank you for using my program!\n\n");

}else if(result>=18.50 && result<22.90)
{
    printf("\nYour BMI is %.2f\n\n",result);
    printf("You are healthy! Keep eating healthy\n\n");
    printf("Thank you for using my program!\n\n");

}else if(result>=22.90 && result<24.90)
{
    printf("\nYour BMI is %.2f\n\n",result);
    printf("You are a little overweight! Avoid eating some fat and an oil\n\n");
    printf("Thank you for using my program!\n\n");

}else if(result>=24.90 && result<29.90)
{
    printf("\nYour BMI is %.2f\n\n",result);
    printf("You are overweight! Avoid eating fat and do exercise often\n\n");
    printf("Thank you for using my program!\n\n");

}else if(result>=29.90)
{
    printf("\nYour BMI is %.2f\n\n",result);
    printf("You are obese! Do exercise everyday and eat carefully!\n\n");
    printf("Thank you for using my program!\n\n");

}else
{
    printf("Error occured!!");
}
return 0;

}

PaoPaoMC
  • 77
  • 1
  • 1
  • 9
  • why to enter char or string when someone is asking weight and that to in float [format specifier](https://www.le.ac.uk/users/rjm1/cotter/page_30.htm) – wrangler Aug 13 '17 at 00:54

2 Answers2

5

scanf will return the number of values read in, so should be one in your example, as you are reading in a single float value.

You could check that scanf returns that value and if it doesn't do some kind of recovery such as reading until end of line:

while(scanf("%f",&height) != 1)
{
  int c;
  while((c = getchar()) != '\n' && c != EOF)
    ;
  printf("Enter your weight as kilogram...\n\n> ");
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
lostbard
  • 5,065
  • 1
  • 15
  • 17
  • 1
    This is the correct approach, though it would be better if the while-loop broke with a retained error condition in the event of reaching EOF rather than a newline. Regardless, uptick-worthy. – WhozCraig Aug 13 '17 at 01:18
0

To pass to the next argument add an fflush(stdin) after the scanf :

scanf("%f",&weight);

  fflush(stdin);

and you need to controle your input like this :

if (scanf("%lf", &weight) == 1)
    printf("It's float: %f\n", weight);
else
    printf("It's NOT float ... \n");
Malek Zarkouna
  • 943
  • 10
  • 21
  • 1
    *"add an fflush(stdin)..."* - or *don't*. Behavior for that is *not* defined by the language standard or its library. `fflush` should only be invoked on *output* streams, or input/output streams where the last operations was an *output*. – WhozCraig Aug 13 '17 at 01:10
  • true , but with it he can passes to the next "scanf" and that's what he wants i guess . – Malek Zarkouna Aug 13 '17 at 01:13
  • 1
    See [Using `fflush(stdin)`](http://stackoverflow.com/questions/2979209/using-fflushstdin) for the nuances, but be cautious about using it — and don't use it off the Windows platform. – Jonathan Leffler Aug 13 '17 at 02:43