用户输入零以表示 C 中输入的结束
Posted
技术标签:
【中文标题】用户输入零以表示 C 中输入的结束【英文标题】:user entering zero to signal the end of input in C 【发布时间】:2019-03-08 03:45:08 【问题描述】:在您将我的问题标记为重复之前,我已经在研究这些类似的问题和答案:
c : How to signal end of input
Stop a for loop when user is finished entering input in c
根据上述参考问题,我的代码应该是正确的,但由于某种原因,我无法让它跳出循环并在用户输入 int(例如零或减一)时开始打印输出。
为了记录,这是一个学校作业,我暂时不能使用'fgets',我们只能使用'scanf'
我真的希望有人可以在这里为我指明正确的方向。我要做的就是让用户输入未指定数量的整数并将它们存储在数组中。我知道允许的最大值数,这就是我使用数组的原因,但是我想让用户可以选择通过输入零或负一来表示输入结束。我知道它应该非常简单,但尽我所能,除非用户输入一个字符,否则我无法让它工作。
我的代码如下:
int i = 0;
int j = 0;
int full_sequence[200] = 0;
int numElements = 0;
printf("enter sequence of integers: \n");
for (i = 0; i < 200; i++)
if (scanf(" %d", &full_sequence[i]) ==0)
break;
//i've also tried setting i to the maximum index instead of using break, neither of which worked;
else
scanf(" %d", &full_sequence[i]);
numElements++;
for (j = 0; j < numElements; j++)
printf("\nend of input *********************");
printf("\nArray element %d is: %d", j, full_sequence[j]);
printf("\nthe number of non-zero elements is: %d", numElements);
return 0;
我还尝试用 while 循环替换 for 循环,并尝试使用负数作为输入结束的信号,如下所示:
while (testValue >= 0)
scanf("%d", &testValue);
scanf("%d", &full_sequence[i]);
i++;
numElements++;
仍然没有,控制台似乎继续期待输入,直到我输入一个字母字符,此时循环最终退出并打印我的输出。奇怪的是,scanf 函数似乎只捕获了我输入的所有其他数字。
如果我尝试重写 for 循环以在将 scanf 项添加到我的数组之前也使用测试值来验证它,类似于我在 while 循环中使用它的方式,它如下所示:
for (i = 0; i < 100; i++)
scanf("%d", &testValue);
if (testValue == 0)
break;
else
scanf("%d", &full_sequence[i]);
numElements++;
还是什么都没有。
【问题讨论】:
我还被告知 scanf 在正确扫描项目时返回 1,因此我可以用某些东西替换 if 语句:scanf(" %d", &full_sequence[i]) ==0)像 scanf("%d", &full_sequence[i] != 1) 但这也不起作用。我想它提出了一个问题,我如何引用存储在给定数组索引处的 int 值,而不是返回的值来指示 scanf 函数本身的成功? 尝试养成在使用变量的地方声明变量的习惯,以便明确它们的用途。例如:for (int i = 0; ...)
而不是提前声明i
。这清楚地表明i
是什么类型,以及它的作用域。
@tadman 好点,谢谢
【参考方案1】:
#include <stddef.h> // size_t
#include <stdio.h> // puts(), printf(), scanf()
enum max_elements = 200 ; // don't use magic numers, give them a name
int main(void)
int full_sequence[max_elements] = 0 ;
size_t num_elements = 0; // the type for sizes of objects in memory and
// indexes into them is size_t, not int
puts("Enter sequence of integers:");
for (size_t i = 0;
i < max_elements // as long as the current index is smaller than the size
&& scanf("%d", &full_sequence[i]) == 1 // an integer could sucessfully be read
&& full_sequence[i] != 0; // and the integer read is not 0
++i) // increase the index
num_elements = i + 1; // keep count of the number of elements
puts("\nEnd of input *********************\n");
for (size_t i = 0; i < num_elements; ++i)
printf("Array element %zu is: %d\n", i, full_sequence[i]);
printf("\nThe number of non-zero elements is: %zu\n", num_elements);
【讨论】:
感谢您的回复,特别是对我不知道的“size_t”进行了教育。您的回答对我帮助很大,再次感谢【参考方案2】:因为还有另一个与事物的编码结束相关的答案:用户传统上在控制台程序中通过键入文件结束的控制代码来表示输入结束。这是 UNIX 上的 control-D,因此是 Linux 和 OS X,或 Windows 上的 control-Z(以及它的前身可以追溯到 8 位时代)。
【讨论】:
当stdin
到达EOF
时,std::cin
变得无法使用。
@Swordfish 你的答案正确地检查了这两个条件(以及填充数组)。
是的,我只是反对 EOF
作为分隔输入的唯一选择。
@Swordfish 如您所知,使用cin
输入直到文件结束的传统方式是while (std::cin >> x)
,因为流输入运算符的返回值是一个流(通过引用)在遇到 EOF 后,流的计算结果为布尔值 false
。
看不到你的观点 int foo; std::cin >> foo
不仅对 EOF
失败。以上是关于用户输入零以表示 C 中输入的结束的主要内容,如果未能解决你的问题,请参考以下文章
编程接受用户输入的若干整数,一-1标志输入的结束,求出其中的最大值,最小值和平均值