2
#include <stdio.h>
#pragma warning(disable:4996)

//Counting GPA
int main()
{
    float finalGrade, totalQualityPoints = 0, totalHours = 0;
    int hours, qualityPoints, subjectsCount, points = 0;
    char gradeLetter;

    printf("How many subjects do you have? ");
    scanf("%d", &subjectsCount);

    for (int i = 1; i <= subjectsCount; i++) {

        printf("\nEnter the number of hours for your %d subject: ", i);
        scanf("%d", &hours);
        printf("Enter the grade letter for your %d subject: ", i);
        scanf("%s", &gradeLetter);
        printf("\n");
        
        if (gradeLetter == 'A' || gradeLetter == 'a') {
            points = 4;
        } else if (gradeLetter == 'B' || gradeLetter == 'b') {
            points = 3;
        } else if (gradeLetter == 'C' || gradeLetter == 'c') {
            points = 2;
        } else if (gradeLetter == 'D' || gradeLetter == 'd') {
            points = 1;
        } else if (gradeLetter == 'F' || gradeLetter == 'f') {
            points = 0;
        }
        
        //At each iteration, this variable adds up the previous value for each subject
        qualityPoints = hours * points;
        totalHours += hours;
        totalQualityPoints += qualityPoints;

    }

    printf("\nTotal hours: %.2f Total QP: %.2f ",totalHours, totalQualityPoints);

    finalGrade = totalQualityPoints / totalHours;
    printf("\nYour GPA is: %.2f ", finalGrade);
}

Run-Time Check Failure #2 - Stack around the variable 'gradeLetter' was corrupted. Visual Studio 2019. How to solve this problem?

The output to the screen is carried out, the program actually reads and outputs gpa. But at the end the compiler outputs a warning

2 Answers2

1

regarding:

scanf("%s", &gradeLetter);
  1. is expecting to read a single character, not a string
  2. needs to consume/discard any left over '\n' left in stdin

Suggest:

scanf(" %c", &gradeLetter);

Notice the use of the %c input format conversion specifier

Notice the space before the conversion specifier to consume any leading 'white space' like the left over '\n' in stdin

regarding the group of statements similar to:

if (gradeLetter == 'A' || gradeLetter == 'a') {

Suggest replacing that whole group with the single statement:

#include <ctype.h>
....
points = 5 - (tolower( gradeLetter ) - 'a' );
....
user3629249
  • 16,402
  • 1
  • 16
  • 17
0

In the line scanf("%s", &gradeLetter); you are trying to read a character string (i.e. a nul-terminated array of char elements) into a single character. For the latter (as in your case), you need the %c format:

scanf("%c", &gradeLetter);

The code, as you have it, will potentially be writing to memory beyond the actual gradeLetter variable (which will be allocated on the stack) and, thus, you will likely be corrupting the stack.

Note: Using the %c format specifier will also likely cause issues with a 'leftover' newline ("enter") character in the input buffer. This issue is addressed here: Reading a single character in C.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Got it! Thanks dude! –  Jul 25 '20 at 16:44
  • this 'correction' will read the left over '\n' from the prior call to `scanf("%d", &hours);` because the `%c` input format conversion specifier does NOT consume/discard leading 'white space' – user3629249 Jul 26 '20 at 16:57
  • @user3629249 Did I say otherwise? The issue is fully addressed in the topic I linked. – Adrian Mole Jul 26 '20 at 17:23
  • It is considered 'off topic' to 'only' link to another page. The answer should include the relevant text from the linked page AND the proposed code in the answer is NOT correct. – user3629249 Jul 26 '20 at 17:36