Read a string with spaces
-
Hi all, I want to read a user input with spaces, say "Hello World". With scanf how can i do that. Because all the time after the space, I cannot read the input.
scanf("%[^\n]", name);
scanf("%s",&name);
fscanf(stdin, "%*s%99[^\n]", name);None of the above solve my issue.
I appreciate your help all the time... CodingLover :)
-
Hi all, I want to read a user input with spaces, say "Hello World". With scanf how can i do that. Because all the time after the space, I cannot read the input.
scanf("%[^\n]", name);
scanf("%s",&name);
fscanf(stdin, "%*s%99[^\n]", name);None of the above solve my issue.
I appreciate your help all the time... CodingLover :)
-
Hi all, I want to read a user input with spaces, say "Hello World". With scanf how can i do that. Because all the time after the space, I cannot read the input.
scanf("%[^\n]", name);
scanf("%s",&name);
fscanf(stdin, "%*s%99[^\n]", name);None of the above solve my issue.
I appreciate your help all the time... CodingLover :)
scanf("%[^\n]", name);
works. If it does not, then check howname
is declared. It should be something likechar name[1024];
«_Superman_» _I love work. It gives me something to do between weekends.
-
scanf("%[^\n]", name);
works. If it does not, then check howname
is declared. It should be something likechar name[1024];
«_Superman_» _I love work. It gives me something to do between weekends.
I did exactly the same, but didn't work mate.
I appreciate your help all the time... CodingLover :)
-
Simply I tried the following, but it didn't work either
char test[1024];
printf("\nTest ");
gets(test);I appreciate your help all the time... CodingLover :)
-
I did exactly the same, but didn't work mate.
I appreciate your help all the time... CodingLover :)
Run this and tell me what you get -
char name[1024];
scanf("%[^\n]", name);
printf("[%s]", name);«_Superman_» _I love work. It gives me something to do between weekends.
-
Run this and tell me what you get -
char name[1024];
scanf("%[^\n]", name);
printf("[%s]", name);«_Superman_» _I love work. It gives me something to do between weekends.
It simply print some garbage characters. Even in debug it'll not looking for the user input.
I appreciate your help all the time... CodingLover :)
-
It simply print some garbage characters. Even in debug it'll not looking for the user input.
I appreciate your help all the time... CodingLover :)
That's strange. It works as expected for me. Are you running it from a console application? If you can give me more details like the environment that you're using and the type of program you're using, I could try to recreate the issue at my end.
«_Superman_» _I love work. It gives me something to do between weekends.
-
That's strange. It works as expected for me. Are you running it from a console application? If you can give me more details like the environment that you're using and the type of program you're using, I could try to recreate the issue at my end.
«_Superman_» _I love work. It gives me something to do between weekends.
Sorry for the late reply! Yes it was a console application. I did this on VS 2008. However when I use cin instead of scanf, works fine.
I appreciate your help all the time... CodingLover :)
-
Sorry for the late reply! Yes it was a console application. I did this on VS 2008. However when I use cin instead of scanf, works fine.
I appreciate your help all the time... CodingLover :)
Same code worked fine for me am also using vs2008