Thread: Data validation

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    10

    Question Data validation

    I'm writting a progrm in which I want to verify the user doesn't input incoherent data. When I code this:

    Code:
    #include<stdio.h>;
    #include<conio.h>;
    main(){
    float var1;
    clrscr();
    scanf("%f",&var1);
    printf("\n\n%f",var1);
    }
    What happens is, if the user enters a valid float value I get the same float value on the output (duh). If he enters a character or a string I always get the same output whatever the input, that is: 149709439917360000.0000 . My question is, how does the computer get this value? Does it convert the characters into ASCII and multiply them or what? Thank you...

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    scanf() will convert all keys typed to float until the first non-digit (and +/- keys) key is typed. So if you type "12.34abc", scanf() will stop converting when it sees the letter 'a' was typed. The rest of the keys remain in the keyboard.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    10

    One half there.

    Ok, so if I type 12.34abc var1 gets the value 12.34. Check. Now, if I type in abc12.34 how does var1 come to get the value 1443268871235100000.0000 whatever....?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    In that case, scanf() will not change the value of var1 -- the number you posted appears because you didn't initialize the varaible with anything. Set it to 0 when declared and you will see the difference. YOu also should check the return value of scanf() to see how many variables were processed or if there was a conversion error. In your example, scanf will most likely return 0 because no variables were converted.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    #include<stdio.h>;
    You shouldn't have semicolons after #includes.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data validation
    By darren78 in forum C Programming
    Replies: 5
    Last Post: 02-19-2009, 12:14 PM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  5. data validation
    By Loraswish in forum C++ Programming
    Replies: 7
    Last Post: 03-13-2003, 10:43 AM