0

I am trying to use scanf() for input. It worked perfectly except when I typed a space. Then it doesn't pick up the string -- why?

char idade;
scanf("%c", &idade);

I tried do to this:

scanf("%[^/n]c", &idade);

But it did not work. For example, when I typed in "Hello world", my string only contained "Hello" All I want is for it to recognize the space and take the full string. How can I do that?

jscs
  • 63,694
  • 13
  • 151
  • 195
user3372120
  • 134
  • 1
  • 10
  • 1
    Could you please update your question? As it is, there is no clear question, and it is really difficult to determine what you are trying to do and what assistance you are asking for. – Jody Hagins Apr 05 '14 at 17:17
  • Sorry I edited now... – user3372120 Apr 05 '14 at 17:28
  • @user3372120 You are reading a character or a string? – ajay Apr 05 '14 at 17:32
  • There is a good discussion about scanning strings with scanf here: http://stackoverflow.com/questions/5406935/reading-a-string-with-scanf – eharo2 Apr 05 '14 at 17:38
  • I still don't understand what you are after. You can use %s to read a string. However, since you are using Obj-C, there are better APIs than sscanf... if you can better describe the problem you are trying to solve. – Jody Hagins Apr 05 '14 at 17:38
  • In my example If I digit 'hello world' in input only have 'hello', so where did the string 'world' was? – user3372120 Apr 05 '14 at 18:40

2 Answers2

2

The %c conversion specifier in the format string of scanf does not skip (read and discard) the leading whitespace characters. If you have a leading whitespace character in the format string, then this means scanf will skip any number of leading whitespace characters in the input which is probably what you want. Assuming you want to read a single character -

char idade;
scanf(" %c", &idade);
   //  ^ note the leading space 

However, if you want to read an input string from stdin, then

char input[50+1];  // max input length 50. +1 for the terminating null byte

// this skips any number of leading whitespace characters and then
// reads at most 50 non-whitespace chars or till a whitespace is encountered - 
// whichever occurs first, then adds a terminating null byte
scanf("%50s", input);  

// if you want to read a input line from the user
// then use fgets. this reads and stores at max 50 chars
// or till it encounters a newline which is also stored - whichever
// occurs first, then it adds a terminating null byte just like scanf
fgets(input, sizeof input, stdin);

Please read the man page of fgets.

ajay
  • 9,402
  • 8
  • 44
  • 71
  • Error! If I digit in the console 'Hello WORLD', in input only have 'hello', so where did the string 'world' was? – user3372120 Apr 05 '14 at 18:40
  • 1
    @user3372120 `"%s"` will scan and discard and then scan and save . Rarely does one want to scan a "C string" (characters followed by a `'\0'`). One usually wants to scan/read a "line" (characters followed by a `'\n'`). To do that try `scanf(" %50[^\n]", input);` or use `fgets()`. – chux - Reinstate Monica Apr 05 '14 at 18:55
  • if it's not asking too much as it would be using the fgets me an example? – user3372120 Apr 05 '14 at 18:57
1

There's a small error in your line

scanf("%[^/n]c", &idade);

It should read

scanf("%[^\n]", &idade);

Backslash '\' is the escape character, not '/'. You have to put '\n' (line feed character) in the exclusion list or else scanf will not know when to stop parsing. Your expression excluded '/' and 'n' as input characters.

Patrick Schlüter
  • 11,394
  • 1
  • 43
  • 48