Search code examples
ciostdin

C Program won't read input from STDIN


I'm writing a basic statistics program as my first in pure C, and for the life of me cannot figure out this one problem. When taking input manually from the command line, it works perfectly. However, when putting in those numbers from an input file, it doesn't read any of them. Here's the source code:

statistics.c:

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[]){

    // Create global variables, introduce program
    int minimum = INT_MAX;
    int maximum = INT_MIN;
    int i = 0;
    int count = 0;
    double total = 0.0;

    printf("%s\n", "Program1");
    printf("%s\n", "Enter nums (0 terminates):");

    scanf("%d", &i);    // Scan in number   
    while (i!=0)
    {
        printf("%d\n", i);          // Print the number just entered
        count++;                    // Increment counter
        total += i;                 // Add to total
        if (i > max) {max = i;}     // Check for maximum
        if (i < min) {min = i;}     // Check for minimum
        scanf("%d", &i);            // Read in the next number
    }
    printf("%s%d\n", "Nums entered: ", counter);
    printf("%s%d%s%d\n", "range: ", min, ", ", max);
    printf("%s%f\n", "mean: ", total/counter);
    return EXIT_SUCCESS;
}

input.txt:

2 3 5 0

When I run ./program in the terminal, and enter those numbers manually, it gives me the expected output. But when I run ./program < input.txt, nothing happens and it gets stuck so that I have to use ^C to kill the process. Any thoughts??


Solution

  • The original code posted defined variables minimum, maximum, count and used variables min, max, counter respectively. Since the original code doesn't compile because of that, all we can be sure of is that your running code was not created from the source originally shown. Please do not post an approximation to the code that is causing you trouble — make sure that the code you post causes the trouble you describe (it compiles; it runs; it produces the claimed output, at least on your machine).

    Here's a spell-corrected version of the code:

    #include <limits.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int min = INT_MAX;
        int max = INT_MIN;
        int i = 0;
        int count = 0;
        double total = 0.0;
    
        printf("%s\n", "Program1");
        printf("%s\n", "Enter nums (0 terminates):");
    
        scanf("%d", &i);
        while (i!=0)
        {
            printf("%d\n", i);
            count++;
            total += i;
            if (i > max) {max = i;}
            if (i < min) {min = i;}
            scanf("%d", &i);
        }
        printf("%s%d\n", "Nums entered: ", count);
        printf("%s%d%s%d\n", "range: ", min, ", ", max);
        printf("%s%f\n", "mean: ", total/count);
        return EXIT_SUCCESS;
    }
    

    When run on the file input.txt containing:

    2 3 5 0
    

    it generates the output:

    Program1
    Enter nums (0 terminates):
    2
    3
    5
    Nums entered: 3
    range: 2, 5
    mean: 3.333333
    

    Consequently, I cannot reproduce your claimed problem, but that may be because I can't see your real code, or perhaps not your real data. If I omit the 0 from the file, then I get an infinite loop with 5 being printed each time.

    Here's an alternative version with more robust input handling; it checks the return value from scanf() and avoids repeating the call, too.

    #include <limits.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int min = INT_MAX;
        int max = INT_MIN;
        int i = 0;
        int count = 0;
        double total = 0.0;
    
        printf("%s\n", "Program1");
        printf("%s\n", "Enter nums (0 terminates):");
    
        while (scanf("%d", &i) == 1 && i != 0)
        {
            printf("%d\n", i);
            count++;
            total += i;
            if (i > max)
                max = i;
            if (i < min)
                min = i;
        }
    
        printf("Nums entered: %d\n", count);
        printf("Range: %d to %d\n", min, max);
        printf("Mean: %f\n", total / count);
        return EXIT_SUCCESS;
    }
    

    This code works correctly on the data file without a 0 as the last number.