scanf returns the number of items converted, so if it succeeds it will be 1. No matter what happens the return value of scanf will not equal USER. Here is a possible change (note the possible):
Code:
#include <stdio.h>
#define USER 123456.0f
int main(void)
{
float user;
do
{
printf("Type in the value of User\n");
scanf("%f", &user);
if(user == USER) /* Very dangerous */
printf("CORRECT\n");
else
printf("WRONG\n");
}
while(user);
return 0;
}
Aside from not checking whether scanf succeeded or not, there's a problem with comparing floating-point values. The values are approximations so comparision for equality is sketchy at best. What you need to do is compare the difference of user and USER to FLT_EPSILON in float.h:
Code:
#include <stdio.h>
#include <float.h>
#include <math.h>
#define USER 123456.0f
int main(void)
{
float user;
do
{
printf("Type in the value of User\n");
scanf("%f", &user);
/* Much better */
if ( fabs ( user - USER ) <= FLT_EPSILON * fabs ( user ) )
printf("CORRECT\n");
else
printf("WRONG\n");
}
while(user);
return 0;
}
-Prelude