why doesn't it print all that I write but only the first word
Because that's the behaviour of that function - according to the man page, %s matches "a sequence of non-white-space characters". If I remember correctly, you should see the terminating new line if it's there in the input. So, all you have to do to read a sequence of characters that includes whitespace is put scanf() in a while() loop that exits once it's spotted a newline. Although you *may* have to use getchar() to skip past whitespace... I'm not too familiar with this family of functions.
Either way, if you play around a bit it shouldn't be too hard.
---------- Post updated at 07:29 AM ---------- Previous update was at 07:23 AM ----------
Oh yeah, I should have mentioned - a perhaps-easier alternative would be to use a regex-style set, e.g:
scanf may be easier(or at least more obvious) but will cause strange problems -- as you've already seen. And there's others too. Try this:
When you enter a number, it works fine. When you enter qwertyuiop, it goes into an infinite loop. This is because scanf only reads one single character, finds that it's not a number, gives up, and puts it back so the data you get the next loop is the same you had last time. You have to fflush(stdin); to get rid of the backed-up garbage.
If you're inputting whole lines, scanf is the wrong function to use.
Often I use fgets and scanf together to avoid that buffer-problem, like this:
Note the extra s, that's intentional. sscanf works just like scanf except it scans strings. This way, if scanf has an error, the data won't remain stuck in the buffer until flushed since it's already been read.
Last edited by Corona688; 03-26-2010 at 12:40 PM..
I have a problem with scanf() for string pointer as member of a struct.
#include <stdio.h>
#include <stdlib.h>
struct Student {
int studentNumber;
int phoneNumber;
char *studentName; //line 7
// char studentName; //line 8
};
int... (10 Replies)
$ cc Array.c
Array.c: In function ‘main’:
Array.c:23: warning: passing argument 1 of ‘scanf’ makes po
Array.c:25: error: expected expression before ‘return’
Array.c:29: error: expected expression before ‘return’
Array.c: At top level:
Array.c:44: error: expected ‘)’ before ‘&’ token... (8 Replies)
Im a newbie to programming language, i found tat there r these function called printf and putchar() as well as scanf and getchar(), im curious abt why do dey hav these 2 different function although dey r doing the same instruction? :confused: (13 Replies)
Hello everyone,
I hope someone is awake to help me on this..
hey How can I do something like this:
The user is asked is asked to enter an int value, but I want to provide a default value on stdout, which they can back space and change it to whatever they want..
for e.g:
Enter the... (4 Replies)
hi all
i have a program in C (Unix Solaris 5.7) and i want to read a string from keyboard, but the "scanf" doesn´t reads spaces.
example:
....
char name;
....
printf("Enter your name: ");
scanf("%s",&name);
printf ("Your name is: %s", name);
and if i write Kevin Costner ... (4 Replies)
hi i am a beginner to C
i have encountered a problem with my assignment, and i have researched it on the internet, but unfortunately i didn't find anything related to that.
i am writing a simple program that takes user's input by prompt command, and parse the whole line into an array of... (1 Reply)
I have a problem reading characters from keyboard with the scanf function.
Here there is a little piece of code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
/* The last 3 libraries are included because in the real program I use some... (4 Replies)