Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-23-2012
PlusA2T
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;     printf("Welcome to BMI calculator.\nPlease type your height first(in Meters):");     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);     printf("\nOk..your BMI is: %.2f\n", BMI);     system("pause");     return 0; }```
• 09-23-2012
laserlight
What does scanf return?
• 09-23-2012
benjaminp
scanf referencescanf - C++ Reference
• 09-23-2012
PlusA2T
Quote:

Originally Posted by laserlight
what does scanf return?

zero..
EVEN haven't print any msg at first time it step in else{}!
• 09-23-2012
benjaminp
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.
• 09-23-2012
PlusA2T
But why do the complier don't run my script?
• 09-23-2012
benjaminp
because the scanf function can not handle a string if you told it that you would enter a floating value.
• 09-23-2012
laserlight
Quote:

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 ){`
• 09-23-2012
PlusA2T
Quote:

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; printf("Welcome to BMI calculator.\nPlease type your height first(in Meters):");     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); printf("\nOk..your BMI is: %.2f\n", BMI); return0; }```

Same problem here..It seems that two lines haven't be executed at first time too..
Attachment 11991
• 09-23-2012
Salem
> 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.
• 09-23-2012
laserlight
Quote:

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.
• 09-23-2012
PlusA2T
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! :confused:
• 09-23-2012
laserlight
Quote:

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.
• 09-24-2012
PlusA2T
Quote:

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;     printf("Welcome to BMI calculator.\nPlease type your height first(in Meters):");     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);     printf("\nOk..your BMI is: %.2f\n", BMI);     return 0; }```
• 09-24-2012
laserlight
Quote:

Originally Posted by PlusA2T
Why do the function "getchar()" make it works! It means I have to catch the "\n" myself? (But seems that scanf will ignore?)

The idea is to discard the invalid input by reading and ignoring the input until and including the newline character.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last