August 5th, 2013, 08: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, 08: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, 08: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, 10: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, 12:06 PM
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, 12:51 PM
Thank you. It works. You saved my day eramit2010 :cool: