Thread: what to clear with clearerr() ?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    35

    what to clear with clearerr() ?

    First, thanks for all the help with my other questions, learning a lot fairly quickly. I read basically when you get an error from input, the error stays until you clear it and enters an infinite loop (this describes EXACTLY the problem I was having). So I'm trying to clear it using clearerr() but I can't seem to figure out what I'm trying to clear. I thought I understood but I don't, I commented out the various combinations I've tried but don't work. The error occurs if I enter something like ff55. If I enter 55lb or 503lb or 4kg the program works fine.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
       int currentNumber;
       int testForEOF;
       char currentUnit[3];
    	
       printf("Enter a number between 0-200 followed by it's units: ");
       testForEOF = scanf("%d%2s", &currentNumber, currentUnit);
       while (testForEOF != -1)
       {
          if (strcmp("lb", currentUnit) == 0 && currentNumber >= 0 && currentNumber <= 200)
          {
             printf("Valid lbs\n");
          }
          else if (strcmp("kg", currentUnit) == 0 && currentNumber >= 0 && currentNumber <= 100)
          {
             printf("Valid kgs\n");
          }
          else
          {
    	/* I've tried
    	clearerr(testForEOF); 
    	clearerr(&testForEOF); 
    	clearerr(*testForEOF);
    	clearerr();
    	clearerr(stdin);
            clearerr(scanf);
    	*/
            printf("Invalid format\n");
          }
          testForEOF = scanf("%d%s", &currentNumber, currentUnit);
       }
    	
       return (0);
    }

  2. #2
    Registered User jephthah's Avatar
    Join Date
    May 2010
    Location
    seattle
    Posts
    49
    to try and clearerr() on anything other than a filestream is pointless... see clearerr.

    your real problem here is that you should cease using "scanf()". learn to use "fgets()" instead.

    using scanf() to get string input is nothing but trouble, unless you really have an advanced understanding of the function.


    .
    Last edited by jephthah; 05-15-2010 at 01:51 AM.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    35
    Quote Originally Posted by jephthah View Post
    to try and clearerr() on anything other than a filestream is pointless... see clearerr.

    your real problem here is that you should cease using "scanf()". learn to use "fgets()" instead.

    using scanf() to get string input is nothing but trouble, unless you really have an advanced understanding of the function.


    .
    I have 2 issues with this the first is that from the link you posted they say
    char *fgets(char *restrict s, int n, FILE *restrict stream); I don't want to read from a file, I want the input to be from the keyboard. The 2nd, Im guessing using fgets dumps the whole input into some sort of array of characters, Im not sure how I could do what Ive done in the middle of my code with checking the units and the integer size in 1 line of code like I have written. ie how can I do
    (strcmp("lb", currentUnit) == 0 && currentNumber >= 0 && currentNumber <= 200) with a giant character array?

  4. #4
    Registered User jephthah's Avatar
    Join Date
    May 2010
    Location
    seattle
    Posts
    49
    stdin is a stream. it's the standard input. typically from teh keyboard.

    Code:
    fgets(buffer, sizeof buffer, stdin);
    once you've got the buffer input, parse it out using various functions like strncmp, strstr, strtok, strtol, strtod, and so forth

    hey, this sure is sounding a lot like the last thread we had this same discussion. were you not paying attention?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    35
    Quote Originally Posted by jephthah View Post
    stdin is a stream. it's the standard input. typically from teh keyboard.

    Code:
    fgets(buffer, sizeof buffer, stdin);
    once you've got the buffer input, parse it out using various functions like strncmp, strstr, strtok, strtol, strtod, and so forth

    hey, this sure is sounding a lot like the last thread we had this same discussion. were you not paying attention?
    I was originally told to use getchar() then to use scanf() now to use fgets() so I don't know how many more inputs C has that I shouldn't be using because they don't work but I'm waiting to be told that I should be using getchar() again so that I'll have come full circle.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    As long as you understand how to correctly use each of them, use whatever one your assignment requires, or whatever works best for you for the task. C doesn't really care what you use, as long as you use it correctly.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    35
    Quote Originally Posted by quzah View Post
    As long as you understand how to correctly use each of them, use whatever one your assignment requires, or whatever works best for you for the task. C doesn't really care what you use, as long as you use it correctly.


    Quzah.
    That's probably the problem, I don't really see why I can't use scanf to do what I want since it basically does exactly what I want except I can't clear the error when I enter hh88. Using fgets seems like I'm going to have to write a hundred lines of quasi regex code to get it to accept 1kg or 200lb or 54kg. If there's no better way than using fgets and writting some huge nasty like 30 case if / switch statement I guess it has to be done, but scanf was so close, is there honestly no way to clear the error being cause by hh66 and I really have to use fgets and write my own regex for it?

  8. #8
    Registered User jephthah's Avatar
    Join Date
    May 2010
    Location
    seattle
    Posts
    49
    you can use scanf all the livelong day, if you like.

    it's a beauty when everything is input perfectly and according to plan.

    just be prepared for it to fail miserably if your user fatfingers the keyboard and inputs something unexpected.

    fgets, on the other hand, will allow you much cleaner control of the user input, because it's all in a string, and you get to be in charge of ensuring it's parsed correctly

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    As per above. You can use whatever you like. You just need to pay attention to the return value of scanf, and if it isn't what it should be, you need to clean up after your user.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    35
    Quote Originally Posted by jephthah View Post
    you can use scanf all the livelong day, if you like.

    it's a beauty when everything is input perfectly and according to plan.

    just be prepared for it to fail miserably if your user fatfingers the keyboard and inputs something unexpected.

    fgets, on the other hand, will allow you much cleaner control of the user input, because it's all in a string, and you get to be in charge of ensuring it's parsed correctly
    So the jist is no I can't use scanf I'm going to have to use fgets and write a parser for it since I know there is going to be purposely bad formatted entries. I feel like all I do in C is code things that high level languages allow you to implement in 1 line. I've been coding for like 15 hours and I've gotten 0 accomplished when I could've written this extremely simple assignment in 2 minutes and in 10 lines of code in any else but this, assembly, and direct machine code.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    35
    Quote Originally Posted by quzah View Post
    As per above. You can use whatever you like. You just need to pay attention to the return value of scanf, and if it isn't what it should be, you need to clean up after your user.


    Quzah.
    That's the heart of my question. How do I clean up after the user? How can I stop the infinite loop when they enter bad input? That's EXACTLY what I'm asking you guys. How do I clean up bad input from scanf????

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're going to have to check to see that your input is correct no matter how you read it. If you read it with fgets, or one character at a time with getchar, you're still going to be ending up checking it all to make sure it's right. There's no avoiding that.

    Edit:
    Code:
    while( 1 )
    {
        if( scanf( "%d", &anint ) == 1 )
        {
            ...do something useful...
        }
        else
        {
            /* chomp out everything up to and including ENTER */
            int c = 0;
            while( (c = getchar()) != '\n'  );
        }
    }
    Basically, you look at your return value, see if it's what you expect, if not, you wipe all the pending input.


    Quzah.
    Last edited by quzah; 05-15-2010 at 02:39 AM.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User jephthah's Avatar
    Join Date
    May 2010
    Location
    seattle
    Posts
    49
    nm. quzah gots it covrered.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    35
    Quote Originally Posted by quzah View Post
    You're going to have to check to see that your input is correct no matter how you read it. If you read it with fgets, or one character at a time with getchar, you're still going to be ending up checking it all to make sure it's right. There's no avoiding that.

    Edit:
    Code:
    while( 1 )
    {
        if( scanf( "%d", &anint ) == 1 )
        {
            ...do something useful...
        }
        else
        {
            /* chomp out everything up to and including ENTER */
            int c = 0;
            while( (c = getchar()) != '\n'  );
        }
    }
    Basically, you look at your return value, see if it's what you expect, if not, you wipe all the pending input.


    Quzah.
    This just goes right over my head, I'm sorry but this makes no sense to me.
    while ( 1 ) // what does that even mean. How would that fit into my code, 1 is never going to change so it's never going to not be 1.

    Code:
    if( scanf( "%d", &anint ) == 1 )
        {
            ...do something useful...
        }
    Im trying to get user input in the form say 56lb. 56lb has both letters and numbers so I would imagine it's never going to do anything useful because it's never going to get to the do something useful part on perfectly valid input.

    Code:
    else
        {
            /* chomp out everything up to and including ENTER */
            int c = 0;
            while( (c = getchar()) != '\n'  );
        }
    Where does c come from. Where is c = getchar() even getting input from? I mean it's going in a loop until it reaches the newline character. Great, so ... I don't see how that does anything even if I knew where it was getting it's input from.

    I mean since both of you agree that does something, obviously I clearly don't understand at all what your pseudo code is trying to say or how I can incorporate that into my code.
    Last edited by fxtdr79; 05-15-2010 at 02:58 AM.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can read them both at the same time:
    Code:
    if( scanf( "%d%s", &number, string ) == 2 )
    See the comment there? See the line right after it?int c = 0;That's where c comes from. getchar returns an int, but it's used to read characters. So you use it to read everything that didn't get stored correctly by you scanf call. Basically, scanf stores the number of conversions it makes. If you want to store two things, then you need to check to see that it actually stored two things.

    In short, you need to get your C book, or your favorite chunk of internet, and actually read what the functions you're trying to use, do. (What arguments they take, what they return, what that means, etc.)


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About clear(). Please help!
    By Antigloss in forum C++ Programming
    Replies: 12
    Last Post: 07-14-2005, 04:02 AM
  2. MFC: Clear Client Area
    By mrafcho001 in forum Windows Programming
    Replies: 2
    Last Post: 06-27-2005, 01:35 PM
  3. How to Clear in Visual C++
    By MyDestiny in forum Windows Programming
    Replies: 4
    Last Post: 03-16-2005, 10:40 PM
  4. Yet another clear screen thread :D
    By kermit in forum Linux Programming
    Replies: 2
    Last Post: 11-20-2003, 05:14 AM
  5. Using a button to clear a list box!
    By Unregistered in forum C++ Programming
    Replies: 13
    Last Post: 08-23-2002, 07:44 PM