Program list 6.5 compflt. C is an example of comparing whether floating point numbers are equal.
The original program is as follows:
// Cmpflt. C -- Floating Point Number comparison # include <math. h> # include <stdio. h> int main (void) {const double answer = 3.14159; double response; printf ("what is the value of PI? \ N "); scanf (" % lf ", & response); While (FABS (response-answer)> 0.0001) {printf (" Try again! \ N "); scanf (" % lf ", & response);} printf (" Close enough! \ N "); Return 0 ;}
When a while loop is input, if a non-numeric character is input, it is in an endless loop.
I modified it again. The modified program is as follows:
// Cmpflt. C -- Floating Point Number comparison # include <math. h> # include <stdio. h> int main (void) {const double answer = 3.14159; int status = 0; double response; printf ("what is the value of PI? \ N "); status = scanf (" % lf ", & response); While (FABS (response-answer)> 0.0001 & status = 1) {printf ("Try again! \ N "); status = scanf (" % lf ", & Response) ;}if (status = 1) printf (" Close enough! \ N "); elseprintf (" You input a wrong Char. \ n "); Return 0 ;}
There is still a problem. If you enter a character, such as "W", the comparison process ends directly. A better way is to determine the statement if it is in the while loop.