![]() |
|
#1
|
|||
|
|||
scanf / clearing input streamHello everyone.
Basically, there are two things I would like to know. Firstly, there is that problem with a program when it encounters another data type (for example, its an int field and you enter a letter) and "crashes" (input via scanf). I know how to solve it, but why does this occur, exactly? And secondly is how does 'while (getchar() != '\n' )' manage to clear the input stream?? It's a loop, yeh...but getchar?! It would be appreciated if you could give me any help on this! If there's anything I don't quite understand yet, it's this. Cheers, Chris. Last edited by dsmith : 05-May-2004 at 14:53.
|
|
#2
|
||||
|
||||
|
Hello zone4, welcome to GIDForums.
Quote:
I am not 100% sure how to answer this. I never use scanf unless I am sure that the input will be what is expected. I instead use fgets and then use atoi or atof to convert the string to the proper value. The best answer that I can give you is that scanf was not created to verify your input. It works very well for what it is intended for, but is not (IMHO) a very good user interactive input function. Quote:
This line loops up to and including the newline character basically throwing away anything in the input stream as it goes. Why does getchar not seem to be right? I am not sure if either of these help a lot. Post again, if you need some better explanation or if I didn't understand your questions well. Cheers, d |
|
#3
|
||||
|
||||
|
Hmmm. That scanf thing sure seems strange. Weird function. but this is what i got from MSDN :
Quote:
so simply just like dsmith stated, it is not built to verify your input. __________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds |
|
#4
|
||||
|
||||
|
Quote:
CPP / C++ / C Code:
2 1) val=2 r=1 5 6t 2) val=5 r=1 3) val=6 r=1 4) val=6 r=0 5) val=6 r=0 6) val=6 r=0 7) val=6 r=0 8) val=6 r=0 9) val=6 r=0 First time thru the loop you enter 2\n into the stream, the program reads 2\n. Second time the stream contains 5 6t\n, the program reads 5 . Third time the stream contains 6t\n, the program reads 6. Forth time the stream contains t\n, the program reads nothing because the t is not a number. scanf() returns a zero -- no values translated. Fifth thru ninth time the stream contains t\n, the program reads nothing because the t is not a number. scanf() does not remove invalid characters. __________________
Age is unimportant -- except in cheese |
|
#5
|
|||
|
|||
|
I was using the return value from scanf to check the number of conversions and if you entered a letter bla bla bla..., but I took dsmiths advice on using fgets and using atoi to convert. I was just wondering, when using scanf, why the program keeps looping itself if you enter a letter when you told it to take in an integer. Fgets seems to be much easier now, though.
and the 'while (getchar() != '\n' )' thing... I was wondering how this manages to clear the input stream in the first place...I don't understand the whole concept of using getchar() to do it! Anyway cheers for the help lads it is greatly appreciated. Chris. |
|
#6
|
|||
|
|||
|
it gets a character from the stream one at a time and then checks if it is the carriage return character and if its not it disposes it and moves on to the next one till it hits carriage return ( '\n' ).
|
|
#7
|
||||
|
||||
|
Quote:
__________________
Age is unimportant -- except in cheese |
|
#8
|
||||
|
||||
|
Quote:
Hi Chris. One of the cool things about these forums is that we are able to generate discussions about these kinds of concepts. Maybe this is something that you can help me with. Why do you not like to use the getchar function to do this? What function do you think should be used? I am just curious as maybe I am missing something here. Cheers, d |
|
#9
|
|||
|
|||
|
nono you're not missing anything - it's just me; i'm getting really confused about it and I don't know why to be honest with you.
It was waltp's example (cheers mate) that made me understand it all I just didn't look at it properly! |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| IP tables | rogermark100 | C Programming Language | 6 | 18-Apr-2004 07:22 |
| Help with array and function input | FearlessFife35 | C Programming Language | 13 | 16-Apr-2004 14:10 |
| Need Help on checking user input | hihellochao | C Programming Language | 5 | 27-Feb-2004 13:30 |
| a C input question | tmike | C Programming Language | 1 | 16-Sep-2003 02:31 |
| Script needed for letting user input a few days of data for tracking and analysis. | tradertt | MySQL / PHP Forum | 3 | 06-Mar-2003 02:54 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The