0

Currently trying to run the program I've written and getting the error "Run-Time Check Failure #2 - Stack around the variable 'midtermgrade' was corrupted."

I have no idea what to do to fix it, very new to programming and got thrown into a C++ course that I certainly did not have the background for.

Using Visual Studio 2017.

Code is supplied below.

#include <iomanip>
#include <iostream>
#include <fstream>
#include <string> 

using namespace std;

double mean(int v[], int size);
// PRE: size of array > 0
// POST: return average of values in array

double stddev(int v[], int size, double m);
// PRE: size of array > 0, 0 <= m <= 100
// POST: return standard deviation of values in array with mean m

char letter(int score, double m, double s);
// PRE: 0 <= score, m, s <= 100
// POST: return letter grade for score with mean m and standard deviation s

string code(string id, string lastname, string firstname);
// PRE: id is seven digit identification
// POST: return code of first letter of last name, first letter of first name, last two digits of id


int main()
{
    ifstream fin;
    ofstream fout;
    fin.open("grades.txt");
    fout.open("grades.txt");
    const int CAPACITY = 25;            // capacity of the array
    string codes[CAPACITY];             // array of id codes
    int midterms[CAPACITY];             // array of midterm grades
    int finals[CAPACITY];               // array of final grades
    string id;                          // 6-digit id code
    string lastname;
    string firstname;                       // Student's year of school
    int midterm;
    int final;
    string idcode;
    double finalmean;                   // Average grade on the final
    double finalstddev;                 // Standard deviation among final grades
    double midtermmean;                 // Average grade on the midterm
    double midtermstddev;               // Standarn deviation among midterm grades
    char finalgrade[CAPACITY];          // An array of the letter grades for the final
    char midtermgrade[CAPACITY];        // An array of the letter grades for the midterm
    int ct = 0;
    while (ct < CAPACITY && fin >> id >> lastname >> firstname >> midterm >> final)
    {
        midterms[ct] = midterm;
        finals[ct] = final;
        idcode = code(id, lastname, firstname);
        codes[ct] = idcode;

        ++ct;
    }
    finalmean = mean(finals, CAPACITY);
    midtermmean = mean(midterms, CAPACITY);
    finalstddev = stddev(finals, CAPACITY, finalmean);
    midtermstddev = stddev(midterms, CAPACITY, midtermmean);
    for (int i = 0; i <= CAPACITY; i++)
    {
        finalgrade[i - 1] = letter(finals[i - 1], finalmean, finalstddev);
        midtermgrade[i - 1] = letter(midterms[i - 1], midtermmean, midtermstddev);
    }
    fout << "Grade Calculations" << endl << "By ----" << endl;
    for (int i = 1; i <= CAPACITY; i++)
    {
        fout << setprecision(1);
        fout << setw(15) << codes[i - 1] << setw(15) << midterms[i - 1]
            << midtermgrade[i - 1] << setw(15) << finals[i - 1]
            << finalgrade[i - 1] << endl;
    }
    fout << endl << endl;
    fout << "The class mean for the midterm exam was a " << midtermmean << endl;
    fout << "The standard deviation amongst the midterm exams was +/- " << midtermstddev << endl;
    fout << endl << endl;
    fout << "The class mean for the final exam was a " << finalmean << endl;
    fout << "The standard deviation amongst the final exams was +/- " << finalstddev << endl;
    fin.close();
    fout.close();
    return 0;

}


string code(string id, string lastname, string firstname)
// PRE: id is seven digit identification
// POST: return code of first letter of last name, first letter of first name, last two digits of id
{
    char firstletterfirst = firstname[0];
    char firstletterlast = lastname[0];
    char fifthdigitid = id[4];
    char sixthdigitid = id[5];
    string idcode = { firstletterfirst, firstletterlast, fifthdigitid, sixthdigitid };
    return idcode;
}

double mean(int v[], int size)
// PRE: size of array > 0
// POST: return average of values in array
{
    int sum = 0;
    for (int i = 1; i <= size; i++)
    {
        sum = sum + v[i - 1];
    }
    double mean = sum / size;
    return mean;
}

double stddev(int v[], int size, double m)
{
    double sumofsquares = 0;
    double spread[25];
    double var;
    double stddev;
    for (int i = 1; i < size; i++)
    {
        spread[i - 1] = (v[i - 1] - m)*(v[i - 1] - m);
        sumofsquares = sumofsquares + spread[i - 1];
    }
    var = sumofsquares / 25;
    stddev = sqrt(var);
    return stddev;
}

char letter(int score, double m, double s)
// PRE: 0 <= score, m, s <= 100
// POST: return letter grade for score with mean m and standard deviation s
{
    char grade;
    if (score < (m - (1.5 * s)))
        grade = 'E';
    else if (score >= (m - (1.5 * s)) && score < (m - (0.5*s)))
        grade = 'D';
    else if (score >= (m - s) && score < m)
        grade = 'C';
    else if (score >= (m+(0.5*s)) && score < (m + 1.5*s))
        grade = 'B';
    else
        grade = 'A';
    return grade;
}
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • 3
    The debugger is trying to tell you that you likely have stepped out of bounds in one of your arrays. – drescherjm Mar 21 '19 at 20:36
  • 6
    `for (int i = 1; i <= CAPACITY; i++)` all of these are wrong. Valid indices are 0 .. CAPACITY -1. Should be `for (int i = 0; i < CAPACITY; i++)` – drescherjm Mar 21 '19 at 20:38
  • 2
    That initial loop where you read input, it looks like you should spend more time learning classes and structures. As well as [the standard C++ containers](https://en.cppreference.com/w/cpp/container) (most specifically [`std::vector`](https://en.cppreference.com/w/cpp/container)). I recommend you [get a couple of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read along your course. – Some programmer dude Mar 21 '19 at 20:40
  • In the IDE, one way to find things like this is to step through your code and look for the point where a stack variable becomes corrupted. Or set a watch on the variable. – Dave S Mar 21 '19 at 20:40
  • Start by initializing your variables. Then crank your compiler warnings to the max and fix everything it warns about. Then use your debugger to step through the code line by line. – Jesper Juhl Mar 21 '19 at 20:43
  • 1
    Ditch all those C-style arrays. Use `std::array` or `std::vector` instead. – Jesper Juhl Mar 21 '19 at 20:46

0 Answers0