Thread: hello , i need help

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    1

    hello , i need help

    hell guys ,
    im new here , i came for a reason , i need help desperately in my project:
    i need to read integer numbers , and if read char , error message should show and then ask to re read numbers , but instead i get infinite loop in while !!
    how can i do it without using :

    fflush malloc realloc math.h ctype.h string.h stdlib.h time.h

    !! please help me ty

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Hello, welcome to the forum. Please show us the code you have so far, and we will help you fix what is wrong. Please be sure to enclose your code in code tags.

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by Emad4U View Post
    hell guys ,
    im new here , i came for a reason , i need help desperately in my project:
    i need to read integer numbers , and if read char , error message should show and then ask to re read numbers , but instead i get infinite loop in while !!
    how can i do it without using :

    fflush malloc realloc math.h ctype.h string.h stdlib.h time.h

    !! please help me ty
    Hey Emad4u and welcome to the forum

    Sure, no worries - what have you come up with so far?
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The simplest way is to read in entire lines at once (e.g. with fgets()) and then trying to parse a number from the line with sscanf(). If that doesn't work, just get another line from the user.

    You can also do this by calling scanf(), and if that fails then read characters one by one (with e.g. getchar()) until you see a newline, then prompt the user and call scanf() all over again. Basically you're throwing away the characters on the line since they're not digits and allowing the user to try again.

    There is a third solution that may have come up in your course, namely using fflush(stdin), but this is not recommended! It's non-standard. It's just a bad idea.

    Really the simplest method looks something like this:
    Code:
    while(scanf("%d", &x) != 1) {
        printf("That doesn't look like an integer to me. Try again.\n");
        while(getchar() != '\n') {}
    }
    scanf returns the number of format specifiers (%d, %f, etc) that were successfully read, so in this case if it returns 1 then all is well. If it doesn't return 1 then just call getchar() over and over to discard everything up to and including the newline. Note that scanf can also return EOF if the standard input stream ends (e.g. it was redirected from a file and the file has ended, or the user types CTRL-Z/CTRL-D), as can getchar(). Technically this should be handled but I've left it out in this illustrative example.

    Finally, here's a thread which discusses other methods of doing this: Safeguarding against non-numerical input
    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