IO problem...

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

Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

IO problem...

 
1
  #1
Nov 18th, 2007
hi, i have the following problem::

i want to read a string from the stdin. The string may contain white spaces so {i think} the scanf family of functions is out of the question... I think the most appropriate function for this case is fgets {if i am wrong please correct me}. Fgets is that it stops when it reads an enter in the input stream and problem arises when i use a menu prior to reading a string that causes an enter to be left in the stream... Is there anyway to circumvent this problem? The only way i found is if i call fgets 2 times in a row{so that the first will discart the enter , and the second will read the string}

here is a sample code::

  1. #include <stdio.h>
  2.  
  3. int Menu()
  4. {
  5.  
  6. int choice = 0; /* menu choice value */
  7. /* with the following loop we force the user to choose
  8. * on of the 3 valid answers: 1, 2, or 3
  9. */
  10.  
  11. do {
  12. fprintf(stdout, "Please choose one of the following:\n"
  13. "1. do1 \n"
  14. "2. do2 \n"
  15. "3. do3 \n");
  16. fscanf(stdin, "%d", &choice);
  17.  
  18. } while ( choice != 1 && choice != 2 && choice != 3);
  19.  
  20. return choice;
  21. }
  22.  
  23.  
  24. int main()
  25. {
  26. int mychoice = Menu();
  27. printf("\n\n\tyour choice was:: %d\n", mychoice);
  28. printf("\nnow lets read a string with fgets\n", mychoice);
  29. char buffer[256];
  30. fgets(buffer, sizeof(buffer), stdin);
  31. fgets(buffer, sizeof(buffer), stdin);
  32. //fscanf(stdin, "%s", buffer); //unfortunatelly it doesn't work because it stops after reading a white space
  33. printf("\nhere is your string ::: %s\n", buffer);
  34.  
  35. return 0;
  36. }

thanks in advance for your help,
nicolas

PS:: also another weird behaviour is when instead of a number{in the menu response} i type a letter, then i get in an infinite loop.Why is this happening?
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: IO problem...

 
1
  #2
Nov 18th, 2007
Use fgets() for everything.

So for example.
  1. int Menu()
  2. {
  3. char buff[BUFSIZ];
  4. int choice = 0; /* menu choice value */
  5. /* with the following loop we force the user to choose
  6. * on of the 3 valid answers: 1, 2, or 3
  7. */
  8.  
  9. do {
  10. fprintf(stdout, "Please choose one of the following:\n"
  11. "1. do1 \n"
  12. "2. do2 \n"
  13. "3. do3 \n");
  14. fgets( buff, sizeof buff, stdin );
  15. if ( sscanf( buff, "%d", &choice ) != 1 ) {
  16. printf( "I asked for a number!\n" );
  17. }
  18. } while ( choice != 1 && choice != 2 && choice != 3);
  19.  
  20. return choice;
  21. }

This also fixes your "what if they type the wrong thing" problem as well.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: IO problem...

 
0
  #3
Nov 18th, 2007
thanks salem, it works! I understand why your code solves the problem "type the wrong thing"...

The thing i can't understand is why scanf can't work together with fgets...i think i 've read somewhere that it isn't the best thing to mix io functions from different families but i can't understand why...
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: IO problem...

 
0
  #4
Nov 18th, 2007
>>it isn't the best thing to mix io functions from different families
but scanf() and fgets() are of the same family of functions, all declared in stdio.h But it is NOT good to use scanf() for string input because it can (potentially) corrupt and possibly crash your program if you enter a string that is longer then the buffer size you want to hold it.
Last edited by Ancient Dragon; Nov 18th, 2007 at 1:59 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: IO problem...

 
1
  #5
Nov 18th, 2007
scanf() uses the bare minimum of characters necessary to perform the conversion, and leaves the first character which cannot be converted on the input stream.

So if you type in
42\n
and scan that with "%d", then 42 will be used and \n will be left.

Now you come along with fgets(), and boom, first character is \n, and it's "thank you and good night from fgets()".
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: IO problem...

 
0
  #6
Nov 18th, 2007
If you are unsure about user input I would recommend reading the tutorial below:

http://www.daniweb.com/tutorials/tutorial45806.html
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: IO problem...

 
0
  #7
Nov 20th, 2007
thank you all for your help...
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: IO problem...

 
0
  #8
Nov 20th, 2007
i read the tutorials on input.Excellent work from Dave Sinkula.

It seems that my biggest mistake concerning fgets was that i didn't know this::
When the incoming text is one less than the size of the buffer, the newline is retained.

PS:: are there any simular tutorials for c++?{ i found the code snipset section but i don't know how can i search it...}

thanks again for all your help,
nicolas
Last edited by n.aggel; Nov 20th, 2007 at 8:56 am.
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: IO problem...

 
0
  #9
Nov 20th, 2007
>PS:: are there any simular tutorials for c++

There is yes.
http://www.daniweb.com/tutorials/tutorial71858.html
And if you want to fall asleep the below is a must read:
http://www.daniweb.com/forums/thread90228.html

If you want to avoid all the theory you could read everything in as strings then convert to numbers wherever necessary.
Last edited by iamthwee; Nov 20th, 2007 at 6:33 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC