This is a BMI Calculator..That's no problem when using correctly.(Typing correct number)
But, if I'm a bad guy and typing some letters…It would show “Don't trick me, Again(in Meters):” repeatedly but wouldn't let me typing anything(Like just skip "scanf")
Code:
```#include<stdio.h>
#include<stdlib.h>

int main(void)
{
float height = 0, weight = 0, BMI = 0;
int OK = 0;
while(!OK){
OK = scanf("%f", &height);
if(height >0 && OK){
printf("\nWell, not so tall :)\nAnd..Your Weight(in Kilograms):");
OK = scanf("%f", &weight);
while(weight <= 0 || !OK) {
printf("\nAgain(in Kilograms):");
OK = scanf("%f", &weight);
}
} else {
printf("\nDon't trick me, Again(in Meters):");
OK = scanf("%f", &height);   //be skipped?
}
if(!height || !weight) OK = 0;
}
BMI = weight / (height * height);
system("pause");
return 0;
}```

2. What does scanf return?

3. scanf referencescanf - C++ Reference

4. Originally Posted by laserlight
what does scanf return?
zero..
EVEN haven't print any msg at first time it step in else{}!

5. your program should write user's input into a string and then if it is correct convert it to a number and then calculate bmi.
wait, i am working on it.

6. But why do the complier don't run my script?

7. because the scanf function can not handle a string if you told it that you would enter a floating value.

8. Originally Posted by PlusA2T
zero..
EVEN haven't print any msg at first time it step in else!
If scanf does return zero, then control would go to your else body. The thing is, scanf can also return EOF. The correct way to check is:
Code:
`if (scanf("%f", &height) == 1 && height > 0 ){`

9. Originally Posted by laserlight
If scanf does return zero, then control would go to your else body. The thing is, scanf can also return EOF. The correct way to check is:
Code:
`if (scanf("%f", &height) == 1 && height > 0 ){`
Code:
```#include<stdio.h>
#include<stdlib.h>

int main(void)
{
float height = 0, weight = 0, BMI = 0;
int OK = 0;
while(!OK){
if (scanf("%f", &height) == 1 && height > 0 ){
printf("\nWell, not so tall :)\nAnd..Your Weight(in Kilograms):");
while(weight <= 0 && scanf("%f", &weight) < 1) {
printf("\nAgain(in Kilograms):");
OK = scanf("%f", &weight);
}
} else {
printf("\nDon't trick me, Again(in Meters):");
scanf("%f", &height);
}
OK = (height || weight) ? 1 : 0;
}
BMI = weight / (height * height);
return0;
}```

Same problem here..It seems that two lines haven't be executed at first time too..

10. > if (scanf("%f", &height) == 1
But if you type in say "hello" then the next time around the loop and you do

if (scanf("%f", &height) == 1
the "hello" will STILL be there, and scanf will fail for exactly the same reason.

You need some way (see the FAQ) of removing some garbage input, and restoring the input stream to some consistent state.

Oh, and when posting code, make sure you "paste as text", so that your code looks like it is in post #1.
Fancy HTML pastes screw the board formatting, resulting in a mess.

11. Originally Posted by PlusA2T
Same problem here..It seems that two lines haven't be executed at first time too..
The problem now is that when input fails, the input is left in the input buffer. You should be removing that input before trying again, e.g.,
Code:
```} else {
int c;
printf("\nDon't trick me, Again(in Meters):");
while ((c = getchar()) != '\n');
}```
Notice that I removed the scanf call here because you will do that call in the next iteration of the while loop. Also, you should get rid of this:
Code:
`OK = (height || weight) ? 1 : 0;`
because it is only OK if you have read both height and weight.

When you read the weight, you should follow the same pattern. At the moment, you are comparing the weight before having read it.

12. Not so understand that..
Do it means scanf() is hard to do this? and where can I found lots of reference of C(like PHP: php.net)? (Not C++ or C#)
Only let user input float and if not then yell user and let user input again.
Can give a example code how to do that?
Thanks!

13. Originally Posted by PlusA2T
Do it means scanf() is hard to do this?
Nope, it just means you need to do a little more work to handle input errors and retry correctly if you want to use scanf. An alternative is to read the input as a string using say, fgets, then parse the string with sscanf. This way, you don't need to discard incorrect input because you have already read it as a string.

14. Originally Posted by laserlight
Nope, it just means you need to do a little more work to handle input errors and retry correctly if you want to use scanf. An alternative is to read the input as a string using say, fgets, then parse the string with sscanf. This way, you don't need to discard incorrect input because you have already read it as a string.
Still no so understand...Why do the function "getchar()" make it works! It means I have to catch the "\n" myself? (But seems that scanf will ignore?)
Lastly, My code here:
Code:
```//
//  BMIC.c
//  BMIC
//
//  Created by PlusA2T on 12/9/23.
//

#include<stdio.h>
#include<stdlib.h>

int main(void)
{
float height = 0, weight = 0, BMI = 0;
int OK = 0;
while(!OK){
while (scanf("%f", &height) != 1 || height <= 0){
if (height <= 0) printf("\nDon't trick me, Again(in Meters):");
while(getchar()!='\n') continue;
}
printf("\nWell, not so tall :)\nAnd..Your Weight(in Kilograms):");
while(scanf("%f", &weight) != 1 || weight <= 0) {
printf("\nAgain(in Kilograms):");
while(getchar() != '\n') continue;
}
OK = (height && weight) ? 1 : 0;
}
BMI = weight / (height * height);