简体   繁体   English

在while循环中使用scanf的问题

[英]Issues with using scanf inside a while loop

I'm brand new to programming. 我是编程的新手。 I 'm working on a homework assignment in order to help us understand scanf and arrays. 我正在从事一项家庭作业,以帮助我们理解scanf和数组。 The program is supposed to ask the user to input an unknown set of numbers. 该程序应该要求用户输入一组未知的数字。 Each set of numbers should be separated by a space like below without hitting enter. 每组数字之间应使用空格隔开,如下所示,请不要按回车键。

14 15 16

The user can also input numbers on a separate line instead using spaces, but again on the last number inputed the user isn't supposed to hit enter. 用户也可以在单独的行上输入数字,而不是使用空格,但是在输入的最后一个数字上,用户也不应按回车键。

12 13
44 55
5

The user should hit ctrl-d to indicate end of input. 用户应按ctrl-d指示输入结束。 The program should display the number of elements entered by the user, along with displaying the numbers the user entered. 该程序应显示用户输入的元素数量以及显示用户输入的数量。 I have been reading around and think I have a basic concept of how scanf works, but I am still having some difficulty. 我一直在阅读,认为我对scanf的工作原理有一个基本概念,但是我仍然遇到一些困难。 The code kind of works. 该代码的作品。 However, if the user just enters the numbers on one line they need to hit ctrl-d three times in order for it to exit the loop and display the information. 但是,如果用户仅在一行上输入数字,则需要按ctrl-d 3次才能退出循环并显示信息。
From what I have found online and understand, I think it's not working because the user hasn't hit return, so the input hasn't been flushed into the stdin. 根据我在网上找到并了解的信息,我认为它不起作用是因为用户没有按回车键,因此输入尚未刷新到标准输入中。 So if I'm understanding correctly, the first time I hit ctrl-d it while flush the input. 因此,如果我理解正确,那么在刷新输入时第一次按ctrl-d即可。 Then the second time I hit ctrl-d it will finally put the EOF into the stream and the third time it will finally read the -1 produced by the EOF and exit the loop. 然后第二次我按ctrl-d,它将最终将EOF放入流中,第三次它将最终读取EOF产生的-1并退出循环。 Is there anyway to force the input stream once ctrl-d is entered. 无论如何,一旦输入ctrl -d,就会强制输入流。

#include <stdio.h>
int main()
{
    int numbers[20];
    int i = 0, count, result, n;
    int flag = 0;

    printf("Please enter a seiries of numbers:\n");
    while (flag == 0)
    {
        result = scanf("%d", &n);  //scan user input into n variable along with getting scanf return value and storing in result variable
        printf("result =%i \n", result); //Just printing scanf return value to insure it doing what I think it should be doing
        if (result == 1) 
        {
            numbers[i] = n; //if scanf return value is 1 places value of n into first element of array
            i++;  //used to increment my array
            flag = 0;//keeps value of flag equal to 0 in order to stay in loop
        }

        if(result == -1) //checks to see if result = to -1 should be value returned if cntl +d is entered
        {
            flag = 1; //sets flag to 1 when cntrl +d is entered in order to exit loop.
        }
    }

    for (count = 0 ; count < i ; count++) //loop to print I which is representing number of user inputs and the actual numbers entered by the user.
    {
        printf("\ni= %i numbers= %i\n", i, numbers[count]);
    }
    return 0;
}

I won't give you a solution directly, but will try to help you improve coding in C. The more you work with C the more you will find out that one can write pretty compact code, once the language is mastered. 我不会直接给您提供解决方案,而是会尝试帮助您改善C语言中的编码。使用C语言的工作越多,一旦掌握了该语言,您就会发现可以编写非常紧凑的代码。

You can omit flag because it depends on result . 您可以省略flag因为它取决于result
And you could omit result because it is just the return value of scanf . 您可以省略result因为它只是scanf的返回值。
You can omit n and use numbers array directly. 您可以省略n并直接使用numbers数组。
And you could make use of the preprocessor to use a constant number (often for array sizes as in your case). 而且,您可以利用预处理器来使用常数(通常根据情况使用数组大小​​)。

Have a look at this. 看看这个。 Maybe it helps you get an idea: 也许它可以帮助您获得一个想法:

#include <stdio.h>

#define COUNT 20

main() {

    int numbers[COUNT];
    int i;

    i = 0;
    while (scanf("%d", &numbers[i]) == 1 && i < COUNT)
        printf("\t%d\n", numbers[i++]);
    return 0;
}

PS: I recommend getting acquainted with the different ways of accessing an array and reading about pointers. PS:我建议您熟悉访问数组和阅读指针的不同方法。 The have a very close relationship really. 确实有非常密切的关系。

Address of first element in array : numbers 数组中第一个元素的地址: numbers
Access i th element of array : numbers[i] 访问数组的第i个元素: numbers[i]
Equivalently : *(numbers + i) 等效: *(numbers + i)
Another equivalence : *(i+numbers) 另一个等效项: *(i+numbers)
Surprise, but equivalent again : i[numbers] 令人惊讶,但又相等: i[numbers]
Address of i th element of array : &numbers[i] i个数组元素的地址: &numbers[i]

K&R is a great resource of information and learning. K&R是大量的信息和学习资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM