0

I’m brand new to programming. I ‘m working on a homework assignment in order to help us understand scanf and arrays. 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. 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. 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.
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. 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. Is there anyway to force the input stream once ctrl-d is entered.

#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;
}
4
  • 3
    First tip, write code that humans can read not just compilers. Sep 28, 2015 at 3:34
  • scanf return any -ve number on wrong input , cant guess -1 . So can compare with 0 .
    – ameyCU
    Sep 28, 2015 at 3:39
  • Study idioms more. Code less. int val, n = 0; while (scanf("%d", &val) != EOF) { numbers[n++] = val; } printf("Read %d numbers.\n", n); ought to work fine. Ctrl-D is end of file. Providing it more than once is meaningless in every system I've seen.
    – Gene
    Sep 28, 2015 at 3:47
  • @ameyCU scanf() returns EOF, a negative number, or 0 on wrong input, More specifically, it returns less than the number of non-"%n" specifiers on wrong input. Sep 28, 2015 at 4:13

1 Answer 1

2

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.

You can omit flag because it depends on result.
And you could omit result because it is just the return value of scanf.
You can omit n and use numbers array directly.
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;
}

P.S.: I recommend getting acquainted with the different ways of accessing an array and reading about pointers. The have a very close relationship really.

Address of first element in array : numbers
Access ith element of array : numbers[i]
Equivalently : *(numbers + i)
Another equivalence : *(i+numbers)
Surprise, but equivalent again : i[numbers]
Address of ith element of array : &numbers[i]

K&R is a great resource of information and learning.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.