Hi
how can i take a string as input till EOF??
About EOF
Moderator: Board moderators
About EOF
I like to solve problems.
-
- Experienced poster
- Posts: 136
- Joined: Fri Apr 15, 2005 3:47 pm
- Location: Singapore
- Contact:
Code: Select all
string s;
while (cin >> s) {
//statement
}
if you want to have a blank space(s), u can use :
Code: Select all
char s[255];
while (cin.getline(s,255)) {
//statement
}
![:oops:](./images/smilies/icon_redface.gif)
Anyway u can use this for C :
Code: Select all
char s[255];
while (gets(s)) {
//statement
}
Although as I know gets can be used to uva problems it is not recommended for programming. Just see this(from GCC):
Another way is:
Hope helped
so I would using something else. It's better using fgets like this:/tmp/ccSVOabh.o: In function `main':
gets.c:(.text+0x1e): warning: the `gets' function is dangerous and should not be used.
Code: Select all
char k[200];
while(fgets(k, 200, stdin)) {
printf("%s", k);
}
Code: Select all
char *string;, array[100];
string=array;
while(scanf("%s", string)!=EOF) {
printf("%s\n", string);
}
/*No Comment*/
This code is equally bad (dangerous, whatever) for exactly the same reason as gets(): you don't specify size of buffer to scanf, so a large enough input will cause buffer overflow.KaDeG wrote:Code: Select all
char *string;, array[100]; string=array; while(scanf("%s", string)!=EOF) { printf("%s\n", string); }
(You can specify buffer size with something like scanf("%99s", ...), though)