1
votes

Beginner programming including arrays and I'm having trouble just getting user input for the arrays. The printf functions I've included are just to check whether my arrays are working, the larger program I'm writing just needs to use these two arrays.

The input for the char array seems to work fine, I've tried a couple of different methods. However, the int array doesn't seem to work using the same diversity of methods I've used successfully with the char array. Not sure what I'm missing. Below is the code and the output when I run the program:

int main()
{

char grades[5]; // create array to store letter grades
int hours[5]; // array to store hours

puts("Please enter letter grades:"); // Input letter grades using fgets
fgets(grades, 5, stdin);

printf("Letter grade for course 3 is %c.\n", grades[2]);


int x = 0;

puts("Please enter course hours:\n");
for (x = 0; x < 5; x++)
{
    scanf("%d", &hours[x]);
}

printf("Course hours for course 2 are: %d.\n", hours[1]);

return 0;
}

Output of this code:

Please enter letter grades:
ABCDF <- user input
Letter grade for course 3 is C.
Please enter course hours:

Course hours for course 2 are: -858993460.
Press any key to continue . . .
1
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. - MFisherKDX
fgets should be allowed to read the newline which will be stored in the array, so the array should be at least char grades[7];to allow for the newline and the string terminator. fgets should be passed sizeof grades as the length. As it is, data is left in the input buffer which upsets the following inputs. - Weather Vane
@xing Your comment should really be the answer IMO. - MFisherKDX
1.) Do not use ridiculously small buffers for fgets(), and if it is important for your program flow, check whether a complete line was read (seach for the newline character) - 2.) Almost always, do not use scanf(). You could use sscanf() on a line read by fgets() or some simpler methods, see also my beginners' guide away from scanf(). - user2371524

1 Answers

1
votes

fgets(grades, 5, stdin); captures ABCD of the input leaving F in the input stream. scanf("%d", &hours[x]); can't parse an int from F though it tries five times. Each failure leaves the F in the input stream.
Make buffers large enough for typical input.
Use fgets for all input. Parse with sscanf or others. Use the return of sscanf to make sure the parsing was successful.

#include <stdio.h>

int main( void)
{

    char grades[50] = ""; // create array to store letter grades
    char line[50] = "";
    int hours[5] =  { 0}; // array to store hours
    int result = 0;

    puts("Please enter letter grades:"); // Input letter grades using fgets
    fgets(grades, sizeof grades, stdin);

    printf("Letter grade for course 3 is %c.\n", grades[2]);


    int x = 0;

    for (x = 0; x < 5; x++)
    {
        do {
            printf("Please enter course %d hours:\n", x + 1);
            fgets ( line, sizeof line, stdin);
            result = sscanf( line, "%d", &hours[x]);
            if ( result == EOF) {
                printf ( "EOF\n");
                return 0;
            }
        } while ( result != 1);
    }

    printf("Course hours for course 2 are: %d.\n", hours[1]);

    return 0;
}