运行时检查失败 #2 - 变量周围的堆栈'gpa'已损坏

Run-Time Check Failure #2 - Stack around the variable 'gpa' was corrupted

本文关键字:堆栈 已损坏 gpa 周围 失败 检查 变量 运行时      更新时间:2023-10-16

我有一个c++程序,我一直在工作,它从顺序访问文件显示学生成绩,然后使用相同的文件来计算和显示每个学生的gpa。在调用displayGpa()之后,它显示"运行时检查失败#2 -围绕变量'gpa'的堆栈被损坏。"

这是。cpp文件给我带来麻烦:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include "sRecords.h"
using namespace std;
void sRecords::displayGpa()
{
    string firstName, lastName;
    string grade[4];
    double gpa[4];
    double finalGpa[4];
    ifstream inputFile;
    inputFile.open("studentRecords.txt");
    while (inputFile)
    {
        inputFile >> firstName >> lastName;
        int x = 0;
        while (x <=3)
        {
            inputFile >> grade[x];
            if (grade[x] == "A")
            {
                gpa[x] = 4.00;
            }
            else if (grade[x] == "A-")
            {
                gpa[x] = 3.70;
            }
            else if (grade[x] == "B+")
            {
                gpa[x] = 3.33;
            }
            else if (grade[x] == "B")
            {
                gpa[x] = 3.00;
            }
            else if (grade[x] == "B-")
            {
                gpa[x] = 2.70;
            }
            else if (grade[x] == "C+")
            {
                gpa[x] = 2.30;
            }
            else if (grade[x] == "C")
            {
                gpa[x] = 2.00;
            }
            else if (grade[x] == "C-")
            {
                gpa[x] = 1.70;
            }
            else if (grade[x] == "D+")
            {
                gpa[x] = 1.30;
            }
            else if (grade[x] == "D")
            {
                gpa[x] = 1.00;
            }
            else if (grade[x] == "D-")
            {
                gpa[x] = .70;
            }
            else
            {
                gpa[x] = 0.00;
            }
        x++;
        }
        finalGpa[x] = (gpa[0] + gpa[1] + gpa[2] + gpa[3]) / 4;
        cout << firstName << " " << lastName << " " << finalGpa[x] << endl;
    }
    inputFile.close();
}
void sRecords::displayHeader()
{
    cout << "t-------------------------------" << endl;
    cout << "ttSTUDENT RECORDS" << endl;
    cout << "t-------------------------------" << endl << endl;
}
void sRecords::printRecords()
{
    ifstream inputFile;
    inputFile.open("studentRecords.txt");
    string string1, string2, string3, string4, string5, string6;
    cout << setw(12) << left << "FIRST NAME" << setw(11) << left << "LAST NAME";
    cout << setw(9) << left << "ENGLISH" << left << setw(6) << "MATH";
    cout << setw(9) << left << "SCIENCE" << left << setw(12) << "CHEMISTRY" << endl << endl;
    while (inputFile)
    {
        inputFile >> string1 >> string2 >> string3 >> string4 >> string5 >> string6;
        cout << setw(12) << left << string1 << setw (11) << left << string2; 
        cout << setw(9) << left << string3 << setw(6) << left << string4; 
        cout << setw(9) << left << string5 << setw(12) << left << string6 << endl;
    }
    cout << endl;
    inputFile.close();
}

你有一个无限循环-似乎你搞混了你的内部和外部循环的逻辑。

看起来你需要修改:

            else
            {
                gpa[x] = 0.00;
            } // end of if/else
        } // end of inner while loop
        finalGpa[x] = (gpa[0] + gpa[1] + gpa[2] + gpa[3]) / 4;
        cout << firstName << " " << lastName << " " << finalGpa[x] << endl;
        x++;
    } // end of outer while loop

:

            else
            {
                gpa[x] = 0.00;
            } // end of if/else
            finalGpa[x] = (gpa[0] + gpa[1] + gpa[2] + gpa[3]) / 4;
            cout << firstName << " " << lastName << " " << finalGpa[x] << endl;
            x++;
        } // end of inner while loop
    } // end of outer while loop