So, I'm relatively new to programming and I'm working on a program that allows for input of grades, course hours, and then calculates GPA based on that. But, I'm going step by step, the first thing I'm trying to do is ensure that I can input the data so that it is properly assigned to memory in the two arrays I've created. However, I'm getting some weird output when I run the program. Each array needs to store 5 values, so... 5 grades and then the course hours for those five courses.

#include <stdio.h>

int main()
{

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

    int x; // counter

    for (x = 0; x < 4; ++x) // stores letter grades in array
    {
        puts("Please enter letter grade: ");
        scanf("%c\n", &grades[x]);
    }

    int y;

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

    return 0;
}

So, it seems my first loop works fine for inputting letter grades. There is a weird thing though. After entering the letter grade for the first course it prompts the user again to enter a letter grade (which is fine), it does this for the entry of all five values EXCEPT the second one. There's just a blank line where you can enter the value and then the program carries on. I've been able to print the values of the array and it seems to be taking the data fine, it's just a weird little thing I noticed.

The real problem is the data input for the second array for course hours. It prints the prompt 4 times, and doesn't allow any input of data.

Thanks for the reply. I did the ++y to fulfill the same function as the ++x in my first for loop. I want x (or y) to increment by 1 after each loop is completed so that the input is placed in the proper data location in the array. It starts at 0 and places the data at position '0' in the array, then increments to 1 after its completion - and so on.

It worked for my first loop, I'm confused why it wouldn't also work for my second?

To debug this, add print statements at lines 15 and 23 to show x, y and what else you think you need to see. If you are using a nice IDE you can break on those lines to watch what is going on. While ++x and x++ should be the same in this code, you may occassionally encounter and odd compiler which is why I use the more classic x++, I don't know what compilers and such so it's suspect. You must add debug lines to see what's up.

Also, why the \n on the scanf? Read http://www.cplusplus.com/reference/cstdio/scanf/

I had a printf statement at line 15 before I posted this and it was able to print the array properly. For the second loop, I don't get prompted for any input, it just prints "Please enter course hours: " four times, and then ends the program.

I have the \n at the end of each scanf to just make the program look cleaner when it runs, so that each prompt is on a new line (scanf won't do that automatically, will it? If so, I can remove it).

Also, I'm using Microsoft Visual Studio 2017. I'm in school for this, but the program is very much "figure it out on your own" with very minimal class instruction (which is fine, I'm learning a lot - but that might explain some of the weird coding choices I make). I basically tinker inside the IDE to try and find something to work, read our textbook and try and learn about C a little more online.

Thank you very much for the replies. I'll try to make some of these changes to the code when I have time to work on it later tonight.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.