reading in data (char, int.. etc) without return key

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2006
Posts: 116
Reputation: Mr.UNOwen is an unknown quantity at this point 
Solved Threads: 0
Mr.UNOwen Mr.UNOwen is offline Offline
Junior Poster

reading in data (char, int.. etc) without return key

 
0
  #1
Nov 29th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 1
Reputation: sunhika is an unknown quantity at this point 
Solved Threads: 0
sunhika sunhika is offline Offline
Newbie Poster

Re: reading in data (char, int.. etc) without return key

 
0
  #2
Nov 29th, 2006
Originally Posted by Mr.UNOwen View Post
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.
I think you can use getc()
Last edited by sunhika; Nov 29th, 2006 at 10:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,652
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: reading in data (char, int.. etc) without return key

 
0
  #3
Nov 30th, 2006
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]

  1. int main( )
  2. {
  3. char buffer[BUFSIZ] = { '\0' } ;
  4. char name[8] = { '\0' } ;
  5. fgets( buffer, BUFSIZ, stdin ) ;
  6. sscanf( buffer, "%s", name ) ;
  7. printf( "The name is %s", name ) ;
  8. getchar( ) ;
  9. }

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'.
  1. int i = 0 ;
  2. char name[10] ;
  3. scanf( "%d", &i ) ;
  4. getchar( ) ;
  5. scanf( "%s", name ) ;
  6. 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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: reading in data (char, int.. etc) without return key

 
0
  #4
Nov 30th, 2006
Originally Posted by Mr.UNOwen View Post
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.
It actually depends on what you're reading and how. Some functions leave the \n in the read buffer, others do not. Some functions ignore \n for certain data types, but not for others. Give us an idea what you are reading and how.

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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2322 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC