I know how to clear the buffer
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?Code:int cb; while ((cb = getchar()) != '\n');
Thanks,
Ash
I know how to clear the buffer
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?Code:int cb; while ((cb = getchar()) != '\n');
Thanks,
Ash
use the function flushall()
L GIK wins!!!
Salutes from logicwonder
Enjoy programming
> 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()
The next read of stdin will return the newline.Code:int cb; while ((cb = getchar()) != '\n'); ungetc( cb, stdin );
is there a certain library i'm not including, because flushall is not recognized by my compiler..?
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.
Thanks much Salem! Another quick question....(I know I'm full of them today!):
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.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; }
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)
> 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.
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.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; }
Yes, I was reading up on strol, but thanks for that as well!
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.
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:
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.Code:int foo; int bar; char str[SIZE]; sscanf(str, "%d%n", foo, &bar);
Oh ok, the tutorials I have didn't mention that, thanks
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?
Yes, well done for spotting my deliberate errorOriginally Posted by Ash1981
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...