运行时检查失败 #2 - 变量周围的堆栈'gpa'已损坏
Run-Time Check Failure #2 - Stack around the variable 'gpa' was corrupted
我有一个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
相关文章:
- gdb错误:Backtrace已停止:上一帧与此帧相同(堆栈已损坏?)
- 变量周围的堆栈'...'已损坏
- 运行时检查失败 #2 变量"A"周围的堆栈已损坏
- 运行时检查失败 #2 - 变量"e"周围的堆栈已损坏。发生
- 运行时检查失败 #2 - 变量周围的堆栈'...'已损坏
- 运行时检查失败 #2 - 变量"l1"周围的堆栈已损坏
- 数组变量周围的堆栈已损坏
- 运行时检查失败 - 变量周围的堆栈已损坏
- 调试:运行时检查失败 #2 - 变量"LoggerThread"周围的堆栈已损坏
- 运行时检查失败 #2 - 变量"数字选择"周围的堆栈已损坏
- 运行时间检查:变量周围的堆栈已损坏
- 变量"name"周围的堆栈已损坏C++
- MS 工作室 17 变量周围的堆栈已损坏
- 错误:运行时检查失败 #2 - 变量周围的堆栈已损坏
- C++指针:数组的堆栈已损坏
- 变量周围的堆栈已损坏
- C++ 变量周围的堆栈已损坏
- 运行时检查失败 #2 - 变量周围的堆栈---已损坏
- VS2010和OpenCV:变量周围的堆栈已损坏
- 指针函数参数已损坏,堆栈已损坏