itscharlieb
itscharlieb

Reputation: 313

C: Scanning from stdin

I'm writing code that, when called from the command line, is redirected a file. The lines of the file (which are sent over stdin) are parsed and read. I want to be able to call a function and have it scan an int, but it seems that there are issues with residual data in scanf (I don't actually know if that's the issue, but that's all I can think of). Here is my code:

dataSetAnalysis(data, experiments);
int selection;
while(1){ //always true. The loop only breaks when the user inputs 4.
    printf("DATA SET ANALYSIS\n"
           "1. Show all the data.\n"
           "2. Calculate the average for an experiment.\n
           "3. Calculate the average across all experiments.\n
           "4. Quit.\n"
           "Selection:__");
    switch(selection){
    case 1:
        displayAll(d,e);
        break;
    case 2:
        individualAverage(d,e);
        break;
    case 3:
        allAverage(d);
        break;
    case 4:
        exit(0);
    }
    scanf("%d", &selection);
}

And this is the second half of the main method.

while(fgets(currentLine, 20, ifp) != NULL){ //while there is still data in stdin to be read

    experiments[i] = currentLine; //experiment[i] points to the same value as current line. Each value in experiments[] should contain pointers to different positions in the allocated buffer array.
    currentLine += 20; //currentLine points 20 characters forward in the buffer array.

    int j = 0; //counter for the inner while loop

    while(j<10){ //while j is less than 10. We know that there are 10 data points for each experiment
        scanf("%d ", &intBuffer[j]);
        data[i][j] = intBuffer[j];
        j++;
    }

    numExperiments++; //each path through this loop represents one experiment. Here we increment its value.
    i++;
}

The program loops infinitely when reaching the while loop in dataSetAnalysis() and continues printing "DATA SET ANALYSIS...." without ever stopping to accept more input over stdin. Is the problem with scanning to the selection variable?

Upvotes: 0

Views: 4058

Answers (2)

yogisha
yogisha

Reputation: 102

The problem is that your stdin is not clear, You have to clear your input buffer by iterating till you find an '\n' or an enter hit.

Try use this

while('\n' != getchar()) { }

just before you scanf , it will get rid of the infinite loop

something like

while('\n' != getchar()) {} scanf("%d", selection);

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 753615

The first and fundamental rule is 'always check the return value from an input function' (such as scanf()). They always return some indication of whether they were successful or not, and if they're unsuccessful, you should not use the values that would have been set had the function call been successful.

With scanf() et al, the correct way to use the function is to test that you got the expected number of values converted:

if (scanf("%d", &intBuffer[j]) != 1)
   …handle error…

You also have the format string: "%d " in the code. That doesn't stop until you type a non-white space character after the number. This is confusing, but any white space (blank, tab, newline) in a format string means read optional white space, but the function only knows when it has finished reading white space when a non-white space character is entered.

Upvotes: 0

Related Questions