0

I am using scanf to read a integer n and then read n strings. But it seems does not work. Here is the program:

#include <stdio.h>
#include <string.h>

#define MAX 1000
#define MAX_LEN 81

char str[MAX][MAX_LEN];
int a,i;


int main()
{
        scanf("%d", &a);
        for (i=0; i<a; ++i) {
                scanf("%[^\n]", str[i]);
        }

        for (i=0; i<a; ++i)
                printf("%s\n", str[i]);

}

Using %[^\n] I want to a sentence into a single string. What is the problem?

update: I want to input like this:

 4
 one 
 two
 THREE three
 FOUR four

But in fact, when I input a "4", the program then output 4 blank lines and then exits.

[walle@centos64 ~]$ ./a.out
4
_
_
_
_

where the output I expected is like this:

[walle@centos64 ~]$ ./a.out
4
one
two
THREE three
FOUR four

thans.

3
  • Examples of input , current output and expected output will help clarify your statement Commented Dec 13, 2013 at 6:48
  • You need to swallow the \n after each scanf(), using e.g. getchar(). Commented Dec 13, 2013 at 6:49
  • @Paul R, thanks to your reply. Can you explain a lot? Why there is no need to "swallow" when I use "%s" format? Commented Dec 13, 2013 at 7:03

3 Answers 3

1

You simply need to add a space before specifier in scanf. It will consume \n left in stdin and will make you able to give second input and so on.

Change to scanf(" %[^\n]", str[i]); and printf(" %s\n", str[i]);

Notice the space given in scanf.

Sign up to request clarification or add additional context in comments.

Comments

1

Use fgets other than scanf to get input lines. An extra getchar is there to consume the new line after inputting the integer n.

scanf("%d", &a);
getchar();
for (i=0; i<a; ++i) {
        fgets(str[i], sizeof(str[i]), stdin);
}

for (i=0; i<a; ++i)
        printf("%s", str[i]);

Note that fgets would store the new line in the string as well. Remove it if you don't need it.

4 Comments

that won't work for the input, which i have updated in my questions. thanks.
@FeiXue Yeah, I posted the answer before seeing your edit. Take a look at this version using fgets.
@FeiXue Did you know that scanf is dangerous and should be avoided? Even in the first part of your program where you use scanf to get the integer n. It's better to do it with fgets and sscanf, as it would be more robust.
thank you. I know scanf may has some problems. But in my situation it's safe and suited. thanks again.
0
#include <stdio.h>
#include <string.h>

#define MAX 1000
#define MAX_LEN 81

char str[MAX][MAX_LEN];
int a,i;


int main()
{
        scanf("%d", &a);
        for (i=0; i<a; ++i) {
                scanf("%s[^\n]", str[i]);
                /* scanf("%s", str[i]);*/        /*this line is also OK */
        }

        for (i=0; i<a; ++i)
                printf("%s\n", str[i]);

}


$ gcc -g t.c
$ ./a.out 
2
one
two
one
two

you missed 's' in "%s[^\n]" before "[^\n]".

2 Comments

That won't work if your input contains white space, as the example in the question.
scanf("%s[^\n]", str[i]) is certainly wrong. Suggest scanf(" %[^\n]", str[i]).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.