为类编写程序,仅限于scanf方法。程序接收可以接收任意数量的行作为输入。使用scanf接收多行输入时出现问题。
#include <stdio.h>
int main(){
char s[100];
while(scanf("%[^\n]",s)==1){
printf("%s",s);
}
return 0;
}示例输入:
Here is a line.
Here is another line.这是当前的输出:
Here is a line.我希望我的输出与我的输入相同。使用scanf。
发布于 2013-01-24 14:56:55
尝试此代码并使用Tab键作为分隔符
#include <stdio.h>
int main(){
char s[100];
scanf("%[^\t]",s);
printf("%s",s);
return 0;
}发布于 2013-01-24 13:16:45
我认为你想要的东西是这样的(如果你真的只限于scanf):
#include <stdio.h>
int main(){
char s[100];
while(scanf("%[^\n]%*c",s)==1){
printf("%s\n",s);
}
return 0;
}%*c基本上是要隐藏输入的最后一个字符。
来自man scanf
An optional '*' assignment-suppression character:
scanf() reads input as directed by the conversion specification,
but discards the input. No corresponding pointer argument is
required, and this specification is not included in the count of
successful assignments returned by scanf().编辑:删除了Chris Dodd猛烈抨击的误导性答案:)
发布于 2013-01-24 13:12:28
我给你个提示。
您需要重复scanf操作,直到达到"EOF“条件。
通常的方法是使用
while (!feof(stdin)) {
}构造。
https://stackoverflow.com/questions/14494309
复制相似问题