[英]Run-Time Check Failure #2 - Stack around the variable 'projectAverage' was corrupted
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
double hw = 0, hwTotal = 0, hwAverage[3], id[3], project = 0, projTotal = 0, projectAverage[3], hwProj, finalExam[3], total[3];
int i = 0, j = 0, k = 0;
for (j = 0; j < 3; j++){
cout << "Input student ID: ";
cin >> id[i];
for (i = 0; i < 4; i++){
cout << "Input homework assignment grade: ";
cin >> hw;
hwTotal = hwTotal + hw;
}
hwAverage[j] = hwTotal / 4;
for (i = 0; i < 2; i++){
cout << "Input project grade: ";
cin >> project;
projTotal = projTotal + project;
}
projectAverage[j] = projTotal / 2;
hwProj = (hwAverage[j] + projectAverage[j]) / 2;
cout << "Input final exam grade: ";
cin >> finalExam[j];
total[j] = (finalExam[j] + hwProj) / 2;
}
cout << endl;
for (k = 0; k < 3; k++){
cout << "Student ID: " << id[k] << endl;
cout << "Homework Average: " << hwAverage[k] << endl;
cout << "Project Average: " << projectAverage[k] << endl;
cout << "Final exam grade: " << finalExam[k] << endl;
cout << "Final average: " << total[k] << endl;
cout << endl;
}
return 0;
}
For some reason, when I run this code, my output looks like this: 由于某种原因,当我运行此代码时,我的输出如下所示:
- Input student ID: 123
输入的学生ID:123
- Input homework assignment grade: 100
输入的作业分配等级:100
- Input homework assignment grade: 100
输入的作业分配等级:100
- Input homework assignment grade: 100
输入的作业分配等级:100
- Input homework assignment grade: 100
输入的作业分配等级:100
- Input project grade: 100
投入项目等级:100
- Input project grade: 100
投入项目等级:100
- Input final exam grade: 100
输入期末考试成绩:100
- Input student ID: 12
输入的学生ID:12
- Input homework assignment grade: 40
输入的作业分配等级:40
- Input homework assignment grade: 40
输入的作业分配等级:40
- Input homework assignment grade: 40
输入的作业分配等级:40
- Input homework assignment grade: 40
输入的作业分配等级:40
- Input project grade: 40
投入项目等级:40
- Input project grade: 40
投入项目等级:40
- Input final exam grade: 40
输入期末考试成绩:40
- Input student ID: 1
输入的学生ID:1
- Input homework assignment grade: 20
输入作业分配等级:20
- Input homework assignment grade: 20
输入作业分配等级:20
- Input homework assignment grade: 20
输入作业分配等级:20
- Input homework assignment grade: 20
输入作业分配等级:20
- Input project grade: 20
投入项目等级:20
- Input project grade: 20
投入项目等级:20
Input final exam grade: 20
输入期末考试成绩:20
Student ID: 123
学生证:123
- Homework Average: 100
平均家庭作业:100
- Project Average: 100
平均项目:100
- Final exam grade: 100
期末考试成绩:100
Final average: 100
最终平均值:100
Student ID: -9.25596e+061
学生编号:-9.25596e + 061
- Homework Average: 140
平均家庭作业:140
- Project Average: 140
平均项目:140
- Final exam grade: 40
期末考试成绩:40
Final average: 90
最终平均分:90
Student ID: 1
学生证:1
- Homework Average: 160
平均家庭作业:160
- Project Average: 160
平均项目:160
- Final exam grade: 20
期末考试成绩:20
Final average: 90
最终平均分:90
Press any key to continue .
按任意键继续 。 .
。 .
。
And I get a stack overflow error. 而且我得到一个堆栈溢出错误。 I'm pretty new to C++, and I was making a program that would ask the user for 10 homework assignment grades, 2 project grades, and a final exam grade from 10 students (the numbers in my program are lower so that I could test it easier).
我刚接触C ++,我正在编写一个程序,要求用户提供10个作业分配等级,2个项目等级以及10个学生的期末考试成绩(程序中的数字较低,因此我可以进行测试更容易)。 I was going to average the homework grades and the project grades separately, then average those averages, then average that average with the final exam grade for a total average (sorry about all the averages), but it doesn't seem to be working and I have no idea why.
我要分别对作业成绩和项目成绩进行平均,然后对这些平均值进行平均,然后将其与期末考试成绩进行平均,得出一个总平均数(对不起,所有平均数),但是它似乎没有用,并且我不知道为什么。 Like I said, I'm pretty new to C++, and even newer to arrays, so do you think that any of you can give me some insight as to where I went wrong?
就像我说的那样,我对于C ++还是相当陌生的,甚至对数组来说还是比较新的,所以您认为你们中的任何人都可以给我一些有关我出错的地方的见解吗? Thank you so much!
非常感谢!
Your arrays like hwAverage have 10 elements (from [0] to [9]), but you refer to element [10]. 像hwAverage这样的数组有10个元素(从[0]到[9]),但是您引用的是元素[10]。
Either define more elements or use different index. 定义更多元素或使用不同的索引。
Use of 10
as an array index is wrong for arrays that have 10
elements in them. 对于其中包含
10
元素的数组,将10
用作数组索引是错误的。 That causes undefined behavior. 这会导致不确定的行为。
Also, using the same for
loop variable doesn't sound right. 另外,使用相同的
for
循环变量听起来并不正确。 You can use i
for the outer for
loop and j
for the inner for
loops. 您可以将
i
用于外部for
循环,将j
用于内部for
循环。 As the code stands now, your outer for
loop will never terminate since the value of i
gets reset in the inner loops. 从现在的代码来看,您的外部
for
循环将永远不会终止,因为i
的值会在内部循环中重置。
Use 采用
int i;
int j; // New variable.
for (i = 0; i < 3; i++){
cout << "Input student ID: ";
cin >> id[i];
for (j = 0; j < 4; j++){ // Use j here
cout << "Input homework assignment grade: ";
cin >> hw;
hwTotal = hwTotal + hw;
}
hwAverage[i] = hwTotal / 10;
for (j = 0; j < 2; j++){ // Use j here
cout << "Input project grade: ";
cin >> project;
projTotal = projTotal + project;
}
projectAverage[10] = projTotal / 2; // Fix the index.
// I can't tell what's the right
// index but it has to be 0 - 9.
projectAverage[i] = projTotal / 2; // Perhaps???
hwProj = (hwAverage[10] + projectAverage[10]) / 2; // Fix index here too.
hwProj = (hwAverage[i] + projectAverage[i]) / 2; // Perehaps???
cout << "Input final exam grade: ";
cin >> finalExam[10]; // Fix index here too.
cin >> finalExam[i]; // Perhaps???
total[10] = (finalExam[10] + hwProj) / 2; // Fix index here too.
total[i] = (finalExam[i] + hwProj) / 2; // Perhaps???
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.