Thread: Check for error in scanf()

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    44

    Check for error in scanf()

    if i have a scanf for example:

    scanf("%d",&x);

    this is set to scan in a int data type to varabila x.

    say the user inputs 2.5, i know the 2.5 will be trunkated to 2 but how useing a if statememt coukd i check if an int has been inputed.

    hope the question makes sense...

    thanks in advance....
    Dangerous Dave

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I don't see why you want to do this. If your program is supposed to take an integer and the user enters a float value, that value is truncated into an integer and the input is correct.
    If you want a float and the user enters an integer, once again the value is made to be the type you wanted and the input is correct.

    To answer your question, I don't think you can do that, but if I think of something I'll be sure to let you know.

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

  3. #3
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    You cant, the scanf will stop reading numbers when it hits the '.' so your input will always be integer, you can't test for a float you dont read.
    My advice is don't use scanf at all, especially with number inputs as it will leave the newlines in the input buffer, use fgets instead.
    Code:
    char temp[10], *temp_ptr;
    
    temp_ptr = temp;
    
    printf("\nEnter number ");
    fgets(temp, 10, stdin);
    
    while(*temp_ptr != '\n')
    {
         /* move along string checking for a '.' which would mean the user entered a float */
    }
    Not great, but you see what I mean.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    44
    would i be correct in saying that the scanf function returns a value of 0 if the correct input has been entered and a value of 1 if incorrect input has been entered??

    and if so how would i check for this returned value?
    Dangerous Dave

  5. #5
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    scanf returns the number of succesful conversions it has made, so if you want to read 2 letters scanf will return 2 if succesful.
    Code:
    printf("Enter 2 letters ");
    if(scanf("%c%c", &a, &b) != 2)
         printf("Error");
    I still say don't use scanf though
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I agree, scanf is easy but error prone both in how you code it and in how it functions. In my opinion that makes it doubly bad. fgets is a little more involved, but your code will be better because of it.

    Though I'd be lying if I said I didn't use scanf sometimes when I was lazy or going for compact code.

    p.s. Thanks C_Coder, until now I had no idea what scanf returned, just that it did in fact return a value.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. Check the return status of scanf
    By Taka in forum C Programming
    Replies: 10
    Last Post: 11-01-2006, 06:27 AM
  3. Check application visibility
    By 3saul in forum Linux Programming
    Replies: 2
    Last Post: 02-13-2006, 05:13 PM
  4. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 05:44 PM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM