| | |
reading in data (char, int.. etc) without return key
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2006
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
Does anyone know what is the best function to use if you want to read user input and not have the return key carry over? Everytime I write something that reads in two things, the second read in always ends up being '\n' because the return key carries over to the next input.
Last edited by sunhika; Nov 29th, 2006 at 10:04 pm.
There are a few things you can try out. For eg. you can accept everything as string from the user and parse it to get out the data which you require using [search]sscanf( )[/search]
Of if you still want to use scanf( ) and write programs that way you can just put getchar( ) after each scanf( ) to take care of the stray '\n'.
Hope it helped, bye.
c Syntax (Toggle Plain Text)
int main( ) { char buffer[BUFSIZ] = { '\0' } ; char name[8] = { '\0' } ; fgets( buffer, BUFSIZ, stdin ) ; sscanf( buffer, "%s", name ) ; printf( "The name is %s", name ) ; getchar( ) ; }
Of if you still want to use scanf( ) and write programs that way you can just put getchar( ) after each scanf( ) to take care of the stray '\n'.
c Syntax (Toggle Plain Text)
int i = 0 ; char name[10] ; scanf( "%d", &i ) ; getchar( ) ; scanf( "%s", name ) ; getchar( ) ;
Hope it helped, bye.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
•
•
•
•
Does anyone know what is the best function to use if you want to read user input and not have the return key carry over? Everytime I write something that reads in two things, the second read in always ends up being '\n' because the return key carries over to the next input.
Since you didn't specify, my guess is you're using scanf() to read numbers and characters. This has always been a problem. SOS has a very workable solution if this is the case.
Last edited by WaltP; Nov 30th, 2006 at 4:27 pm.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Reading in data into array (special case, please see) (C++)
- Reading a Binary File to a C++ Class (C++)
- Beginner with C++ and goofy run time problem with DBL Linked List (C++)
- Perl/CGI (Reading Data) Part II (Computer Science)
- adding data into an char array (C++)
Other Threads in the C Forum
- Previous Thread: char*(*(*x)(void))[]
- Next Thread: HELP dynamically created and resized arrays
Views: 2322 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C
* api append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax database directory drawing dynamic executable execv feet fgets file floatingpointvalidation fork frequency function getlogicaldrivestrin givemetehcodez global graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked reversing scheduling segmentationfault send single socketprogramming spoonfeeding standard strchr string student suggestions system test testautomation testing unix urboc user whythiscodecausesegmentationfault win32api windowsapi






