Thread: =( no worky.. help!

  1. #1
    Unregistered
    Guest

    =( no worky.. help!

    i know i am posting alot but i really need to learn if you dont mind.. but anywayz this is not working for me when i enter the correct value it tells me that it is wrong instead of right.. here is the code....

    Code:
    #include <stdio.h>
    
    #define USER 123456
    
    int main(void)
    {
       float user;
       
         do
         {
            printf("Type in the value of User\n");
            if(scanf("%f", &user)== USER)
              printf("CORRECT\n");
            
              else
                   printf("WRONG\n"); 
                   }
                
                while(user);
                
                
                  return 0;
                  
                }
    the correct value suppost to be 123456 and it should say "RIGHT" but when i type in 456213 it still stays "RIGHT" instead of "WRONG"..

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    wow well you know i am sitting here wondering what is this...

    if ( fabs ( user - USER ) <= FLT_EPSILON * fabs ( user ) )

    what is fabs and what is FLT_EPSILON can you please explain thouse commands for me.. sorry but i am still a newbie so if you dont mind explaining it to me in a simple way that i can understand it ... thank you very much

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >what is fabs
    fabs returns the absolute value of it's argument, just an extra precaution against errors.

    >what is FLT_EPSILON
    The official definition is the difference between one and the least value greater than 1 that is representable by the floating-point type. How it works doesn't really matter as much as understanding that comparing floating-point values directly is dangerous because they are approximations and may be equal as far as you are concerned, but not equal to the computer. By using EPSILON in this way you can ask the computer "Is this close enough?".

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mac - Default Lines Endings - fgets() - no worky
    By Dino in forum C Programming
    Replies: 6
    Last Post: 01-30-2008, 11:59 PM
  2. No Worky
    By bumfluff in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2006, 11:13 AM
  3. _beginthread when function has more than one parameter
    By blundstrom in forum C Programming
    Replies: 5
    Last Post: 10-04-2001, 12:28 PM