0

The program will continuously scan numbers into an array, where the array will be no larger than 100 values.

However, the counter in the first while loop, 'i', continues to count up to 99 despite the program exiting after the 3rd value is entered. Thus, when the second while loop is initiated, it prints the values starting from 99.

How could you make the counter stop when the loop is exited?

This is a homework assignment and the first time touching arrays in C.

I have already tried using an if statement to exclude the zeros for all array values not necessary but sometimes 0 can be entered into the array and needs to be printed.

#include <stdio.h>

int main(void) {

    printf("Enter numbers forwards:\n");
    int numbers[99] = {0};

    // Components of the scanning while loop
    int i = 0;
    while (i <= 98) {
        scanf("%d", &numbers[i]);
        i = i + 1;
    }

    // Components of while loop
    int counter = i - 1;

    printf("Reversed:\n");

    while (counter >= 0) {
        printf("%d\n", numbers[counter]);
        counter--;
        /*if (numbers[counter] == 0) {
            counter--;
        } else {
            printf("%d\n", numbers[counter]);
            counter--;
        }*/
}

Expected Results: Enter numbers forwards: 10 20 30 40 50 CTRL-D Reversed: 50 40 30 20 10

Actual Results: Enter numbers forwards: 10 20 30 40 50 CTRL-D Reversed: 0 0 0 ... 50 40 30 20 10

sovietek
  • 3
  • 5
  • 3
    Hint: take a look at `scanf`'s return value and see if you can change your first while loop's condition to take advantage of it. – Ammar Askar Oct 07 '19 at 07:47
  • When calling `scanf`, *always* check its return value. If you're trying to match one input, and `scanf` doesn't report it has matched 1 inout, something has gone wrong. – Steve Summit Oct 07 '19 at 10:49

2 Answers2

1

when ctrl+d is pressed it generates an end of file or it closes the input stream.even if the end of file is reached if it is not handled explicitly the while loop will run until i<=98. when input stream is closed with ctrl+d the scanf returns the EOF flag while trying to read.

To achieve your goal you have to write your while loop like this:

while (i <= 98) {
    if(scanf("%d", &numbers[i])<=0)
        break;
    i = i + 1;
}

// Components of while loop

[ keep in mind end of file is generated with ctrl+z in windows and ctrl+d in linux ]

Reshad
  • 220
  • 5
  • 19
  • Checking `scanf`'s return value is important, yes, and EOF is the condition the OP asked about -- but testing for EOF like this is poor, too. In particular, try this code on input containing a single `x` character anywhere in it. – Steve Summit Oct 07 '19 at 10:48
  • I assumed that your input will only contain integers. Do you want to stop the loop if anything other than intger is given in input? if yes than the edited could should work for any input char or ctrl+d – Reshad Oct 07 '19 at 12:48
  • @Reshad It is meant to fill an array with the number of scanf you input. So if I enter 10 20 30 40 50, the array should contain only these numbers. I'm at a loss as to how to do this. – sovietek Oct 07 '19 at 13:09
0

After researching how scanf returns values, and with other research into similar projects, this code runs perfectly for what it intended. Thanks for pointing out the scanf return values and how they can be utilised!

#include <stdio.h>

int main(void) {

    printf("Enter numbers forwards:\n");

    int userInteger = 0;
    int i = 0;
    int numbers[100] = {0};

    // As suggested, if the user inputs 1 integer into scanf, it will return 1
    // Therefore, as long as integers are being read into the program, the 
    // while loop will continue to run. It will stop when a non-integer is 
    // input.
    // When hitting CTRL-D, this will stop the loop here and stop the counter
    while (scanf("%d", &userInteger) == 1) {
        numbers[i] = userInteger;
        i++;
    } 

    printf("Reversed:\n");

    // Due to the final i being counted in the previous
    // loop before failure
    i = i - 1; 

    while (i >= 0) {
        printf("%d\n", numbers[i]);
        i--;
    }
    return 0;
}

The post that helped with this project is here!

sovietek
  • 3
  • 5