用ctrl+d结束while循环,scanf?
End while loop with ctrl+d, scanf?
我希望询问用户 "how many circles" 他们想写,直到用户决定用 (Ctrl+d) 哪个是 EOF?
附加问题:如果我写一封信,例如"k",它会向圈子发送垃圾邮件。我该如何更改?
#include <stdio.h>
int main ()
{
int i;
int x;
printf("\nHow many circles do you want to write?\n");
scanf("%d", &x);
while(x != EOF)
{
for (i = 1; i <= x; i = i + 1)
{
putchar('o');
}
printf("\nHow many circles do you want to write?"
"(to end program click ctrl+d at the same time!))\n");
scanf("%d", &x);
}
printf("\n\n Bye! \n\n");
return 0;
}
根据 man page,scanf()
将 return
EOF,而不是 scan
EOF 到 x
作为值。
Return Value
These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
The value EOF
is returned if the end of input is reached before either the first successful conversion or a matching failure occurs......
此外,
if I write a letter for example "k" it will spam out circles, how do I change that?
如果输入一个char
值,会导致匹配失败,你的情况是scanf()
returns0
,而不是1
].
因此,总的来说,您必须收集 scanf()
的 return 值 并检查该值是否符合要求的条件。您可以将代码更改为
int retval = 0;
while ((retval = scanf("%d", &x))!= EOF && (retval == 1))
如果您允许 #include ,则有两个方便的函数 bool kbhit() 和 char getch()。
所以你可以写
char c=0;
if(kbhit()) c = getch();
if(c== whatever code ctrl+d returns) x=EOF;
提示:当你输入字母而不是数字时,看看 scanf(%d,&x) returns 是什么。
您的程序最大的问题是 scanf
不会将 EOF
读入变量。但是,仅仅解决这个问题并不能使您的程序完全正确,因为您的代码中还有其他问题:
- 您的代码会自我重复 - 如果可能,您应该统一处理第一次迭代与后续迭代的代码。
- 您的代码将不会处理无效输入 - 当最终用户输入非数字数据时,您的程序将进入无限循环。
- 您的代码遵循 C 的旧样式 - 在顶部声明所有变量已经超过十五年了。您应该在循环内声明循环变量。
以下是解决所有这些缺点的方法:
int x;
for (;;) {
printf("\nHow many circles do you want to write? (to end the program click Ctrl+D at the same time!)\n");
int res = scanf("%d", &x);
if (res == EOF) break;
if (res == 1) {
... // Draw x circles here
} else {
printf("Invalid input is ignored.\n");
scanf("%*[^\n]");
}
}
printf("\n\n Bye! \n\n");
return 0;
您可以通过字符输入读取字符:
#include <stdio.h>
int main ()
{
int i;
int x = 0;
int nb = 0;
while(x != EOF)
{
printf("\nHow many circles do you want to write?\n");
nb = 0;
for (x = getchar(); x != '\n'; x = getchar()) {
if (x == EOF)
goto end;
if (x >= '0' && x <= '9') {
nb = nb * 10 + x - '0';
}
}
for (i = 0; i < nb; i++)
{
putchar('o');
}
}
end:
printf("\n\n Bye! \n\n");
return 0;
}
我希望询问用户 "how many circles" 他们想写,直到用户决定用 (Ctrl+d) 哪个是 EOF?
附加问题:如果我写一封信,例如"k",它会向圈子发送垃圾邮件。我该如何更改?
#include <stdio.h>
int main ()
{
int i;
int x;
printf("\nHow many circles do you want to write?\n");
scanf("%d", &x);
while(x != EOF)
{
for (i = 1; i <= x; i = i + 1)
{
putchar('o');
}
printf("\nHow many circles do you want to write?"
"(to end program click ctrl+d at the same time!))\n");
scanf("%d", &x);
}
printf("\n\n Bye! \n\n");
return 0;
}
根据 man page,scanf()
将 return
EOF,而不是 scan
EOF 到 x
作为值。
Return Value
These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
The value
EOF
is returned if the end of input is reached before either the first successful conversion or a matching failure occurs......
此外,
if I write a letter for example "k" it will spam out circles, how do I change that?
如果输入一个char
值,会导致匹配失败,你的情况是scanf()
returns0
,而不是1
].
因此,总的来说,您必须收集 scanf()
的 return 值 并检查该值是否符合要求的条件。您可以将代码更改为
int retval = 0;
while ((retval = scanf("%d", &x))!= EOF && (retval == 1))
如果您允许 #include ,则有两个方便的函数 bool kbhit() 和 char getch()。 所以你可以写
char c=0;
if(kbhit()) c = getch();
if(c== whatever code ctrl+d returns) x=EOF;
提示:当你输入字母而不是数字时,看看 scanf(%d,&x) returns 是什么。
您的程序最大的问题是 scanf
不会将 EOF
读入变量。但是,仅仅解决这个问题并不能使您的程序完全正确,因为您的代码中还有其他问题:
- 您的代码会自我重复 - 如果可能,您应该统一处理第一次迭代与后续迭代的代码。
- 您的代码将不会处理无效输入 - 当最终用户输入非数字数据时,您的程序将进入无限循环。
- 您的代码遵循 C 的旧样式 - 在顶部声明所有变量已经超过十五年了。您应该在循环内声明循环变量。
以下是解决所有这些缺点的方法:
int x;
for (;;) {
printf("\nHow many circles do you want to write? (to end the program click Ctrl+D at the same time!)\n");
int res = scanf("%d", &x);
if (res == EOF) break;
if (res == 1) {
... // Draw x circles here
} else {
printf("Invalid input is ignored.\n");
scanf("%*[^\n]");
}
}
printf("\n\n Bye! \n\n");
return 0;
您可以通过字符输入读取字符:
#include <stdio.h>
int main ()
{
int i;
int x = 0;
int nb = 0;
while(x != EOF)
{
printf("\nHow many circles do you want to write?\n");
nb = 0;
for (x = getchar(); x != '\n'; x = getchar()) {
if (x == EOF)
goto end;
if (x >= '0' && x <= '9') {
nb = nb * 10 + x - '0';
}
}
for (i = 0; i < nb; i++)
{
putchar('o');
}
}
end:
printf("\n\n Bye! \n\n");
return 0;
}