August 5th, 2013, 07:01 AM
Want a way to read strings with spaces using the gcc compiler
I'm having a weird problem
i'm trying to read a string from a console with scanf()
but it doesnt read anything. it just skips the entire scanf.
I'm trying it in gcc compiler
I just want to know of a way to read strings with spaces using the gcc compiler Please give me a simple solution. Please don't confuse with buffers and stuff
Thank you in advance
August 5th, 2013, 07:15 AM
there should be another scanf before this one.
due to that scanf of there \n in present in buffer so it takes it.
solve this problem use separator before % in ur scanf
August 5th, 2013, 07:34 AM
I do it so he doesn`t jump over after scanning the string.
scanf("%c", &c); <---to "absorb" the newline character
August 5th, 2013, 09:20 AM
What you're describing works for us. In order to find out why it doesn't seem to work for you, we would need to see what you are actually doing.
Originally Posted by Proff
Show us the code that "doesn't work". A shortened program that still exhibits the problem would do as well. Also tell us exactly what you input to get those results.
August 5th, 2013, 11:06 AM
I suggest that you use a more appropriate (and safer) function than scanf() for this:
char string1 ; // or whatever size you need
fgets( string1, sizeof(string1), stdin ) ;
August 5th, 2013, 11:51 AM
Thank you. It works. You saved my day eramit2010 :cool: