Thread: clearing the buffer - but not the newline

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    119

    clearing the buffer - but not the newline

    I know how to clear the buffer

    Code:
    int cb;
    
    while ((cb = getchar()) != '\n');
    But is there a way to clear it so that when it reads the newline character, it doesn't assign it to cb, but instead leaves it in the buffer?

    Thanks,

    Ash

  2. #2
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    use the function flushall()
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > use the function flushall()
    What sort of non-standard thing is this?

    > but instead leaves it in the buffer?
    If you read one character too many, then you can put one back with ungetc()
    Code:
    int cb;
    while ((cb = getchar()) != '\n');
    ungetc( cb, stdin );
    The next read of stdin will return the newline.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    is there a certain library i'm not including, because flushall is not recognized by my compiler..?

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Also, another quick question if you don't mind, I was using fgets to get a string, and sscanf to parse thru it and pull out an int, and my program is working great, but there is one thing I'd like to change. It is a menu, and you have to enter a number 1 - 3 which corresponds to different choices on the menu. If you just hit 'enter' it does what it should do, and tells you that's not valid. If you hit a number > 3 it does the same, which is good. also, if you enter a character instead of a number, it does the same as well. all that is great, however, if you enter "1fjdskalfje;liwneial;", it will go ahead and select menu option 1. I'd like to have it so that if they type all that crap after the 1, it tells them that that is an invalid selection as well.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Thanks much Salem! Another quick question....(I know I'm full of them today!):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct enemy_info;
    
    int main() {
        enemy_info();
        printf("The dog's health is:%d",hp);
        getchar();
    
    	return 0;
    
    }
    
    struct enemy_info() {
    
           struct enemies {
    
                  int hp;
                  int dam;
    
                  }
    
                  struct enemies dog;
                  struct enemies bear;
    
                  dog.hp = 150;
                  dog.dam = 75;
    
                  bear.hp = 300;
                  bear.dam = 150;
    
                  return;
    
                  }
    I was just screwing around, because one day (hopefully), I'm gonna program my own text based rpg in c. Yeah, I know text is "outdated", but I like it. I like letting my imagination provide the graphics sometimes . Anyway, the code above doesn't compile. I'm trying to return a structure through a function, I read it's possible on this tutorial page, but they didn't give an example, so I had to guess on how you did it, and obviously I guessed wrong. If I can't do it that way, I need to figure out a way so that I can have all the enemies relevant info (damage, strength, health) in one function, and make it so that depending on what enemy you come across, the program loads up the stats for that particular enemy. Maybe if there's a way I can declare some sort of "global" variable, that can be used outside of just the function it's defined in. Or at least a way to make the function be able to return more than one type of input. In other words, 1 return-type isn't gonna cut it, if I need to return hitpoints and damage and all.

    If all else fails, I'm thinking of having a certain function just for the fighting, and when you call this function, you can put all the relevant info in there (i.e. fight(hp,dam,blah); ), and whatever function calls it can know all those stats....any ideas? (sorry this is no long winded)

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I'd like to have it so that if they type all that crap after the 1, it tells them that that is an invalid selection as well.
    Code:
    #include <stdio.h>
    int main(void)
    {
      char buff[BUFSIZ];
      while ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
        int result, value, nchars;
        result = sscanf( buff, "%d%n", &value, &nchars );
        if ( result == 1 ) {
          if ( buff[nchars] == '\n' ) {
            printf( "You entered %d\n", value );
          } else {
            printf( "Read %d, but there was some other stuff\n", value );
          }
        } else {
          printf( "Whole buffer is junk\n" );
        }
      }
      return 0;
    }
    You can achieve the same effect with strtol() as well, which is actually better than sscanf() in that it also detects numeric overflow in conversions as well.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Yes, I was reading up on strol, but thanks for that as well!

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    I see what you mean above, but can you just make up a letter, like you did with that %n, or does it represent something. Because I've know about %c,%s,%d, %f, (and what they represent, char, string, int) and the like, but I don't recall a %n.

  10. #10
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Why don't you read up on your scanf family docs?

    %n expects a pointer to int argument, to store the number of characters scanned so far.

    So:
    Code:
    int foo;
    int bar;
    char str[SIZE];
    sscanf(str, "%d%n", foo, &bar);
    The above read a number into "foo" and tell you how many characters along the string you scanned by putting that in bar. Example, if str contains "1234 abcdef" and you did the above, you would get a value of 1234 in foo and 4 in bar, because the %d converted 1234 to an integer, which is 4 characters long.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Oh ok, the tutorials I have didn't mention that, thanks

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Now rereading what Salem wrote, after what you just told me, I see exactly how that works - I can't thank you both enough.

    Not to nitpick, but shouldn't there be a unary operator (&) in front of 'foo' up there?

  13. #13
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by Ash1981
    Not to nitpick, but shouldn't there be a unary operator (&) in front of 'foo' up there?
    Yes, well done for spotting my deliberate error

  14. #14
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    heh, thanks, i'm gonna go grab some z's now. btw, up above, i didn't mean what i wrote about passing a structure through a function...I was thinking of something else i'd tried, and wrote completly the wrong thing with regard to that. sleep depravation i guess....anyway, you guys take care, cya tomorrow...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. text input buffer clearing
    By red_Marvin in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2003, 03:17 PM
  4. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM
  5. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 09:28 AM