Gee Dub
Gee Dub

Reputation: 1

Why is my program only taking one input before completing?

This program uses an array and three functions to read inputs, sum up the ones and tens place of inputted integers, and compute the average of input integers. Why can't I get more than one input that is a positive integer? The program runs after one input that is within the limits of integers that are valid.

#include <stdio.h>

int read_data(int Ar[]);
void comp_sums(int Ar[], int size); // prototypes
double comp_avg(int Ar[], int size);

int main()
{
    int Ar[100];
    int size;
    double avg;
    size = read_data(Ar);
    comp_sums(Ar, size);
    avg = comp_avg(Ar, size);
    printf("The average of the integers in the array: %lf\n", avg);
}

int read_data(int Ar[])  // reads inputted integers, stores in array
{
    int flag;
    char ch;
    int i,j, num;

    flag = 1;
    i = 0;
    while (flag == 1) {
        printf("Please enter an integer:\n");

        j = scanf("%d", &num);
        if (j != 1) {
            break;
        }

        if (num < 0) {
            continue;
        } else if (num >= 100) {
            flag = 0;
        } else {
            Ar[i] = num;
            i++;
        }
        return i;
    }
}


void comp_sums(int Ar[], int size)   /* computes sum of ones and tens place of the inputted integers into the array*/
{
    int i, j;
    int sum_ones, sum_tens;
    sum_ones = 0;
    sum_tens = 0;
    for (i = 0; i < size; i++) {
        sum_ones += Ar[i] % 10;
        j = Ar[i] / 10;
        sum_tens += j % 10;
    }
    printf("The sum of the ones is: %d\n", sum_ones);
    printf("The sum of the tens is: %d\n", sum_tens);
}

double comp_avg(int Ar[], int size) // computes average of integers
{
    int i, sum;
    double avg;
    sum = 0;
    for (i = 0; i < size; i++) {
        sum += Ar[i];
    }
    avg = (double)size / sum;
    return avg;
}

Upvotes: 0

Views: 103

Answers (2)

Pablo
Pablo

Reputation: 13570

When you take an array as an argument, you have to take the length as well, because you have to check if you are reading/writing out of bounds. Forget for a second that the return is at that incorrect position, the user can input more values than the array can hold and you are doing nothing to prevent the buffer overflow.

So to fix your read_data function:

int read_data(int Ar[], size_t len)
{
    if(Ar == NULL || len == 0)
        return 0;

    int i = 0, j, num;

    // imortant to check the bounds
    while (i < len) {
        printf("Please enter an integer:\n");

        j = scanf("%d", &num);
        if (j != 1) {
            break;
        }

        if (num < 0) {
            continue;
        } else if (num >= 100) {
            break;
        } else {
            Ar[i] = num;
            i++;
        }
    }

    return i;
}

I removed the flag bit because if num >=100, you would be ending the loop anyway, so it's much simpler to do a break. Also the intention is more clearer.

Now you can call it:

int main()
{
    int Ar[100];
    int size;
    double avg;
    size = read_data(Ar, sizeof Ar / sizeof *Ar);
    ...
}

Upvotes: 1

4386427
4386427

Reputation: 44274

The problem is that you call return inside the whileloop. So as soon as you get a valid number, you'll return from the function.

Move it outside the loop. Like:

while (flag == 1) {
    printf("Please enter an integer:\n");

    j = scanf("%d", &num);
    if (j != 1) {
        break;
    }

    if (num < 0) {
        continue;
    } else if (num >= 100) {
        flag = 0;
    } else {
        Ar[i] = num;
        i++;
    }
    // return i;  Incorrect
}
return i;  // Correct

Upvotes: 0

Related Questions