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.
input,current outputandexpected outputwill help clarify your statement\nafter eachscanf(), using e.g.getchar().