简体   繁体   中英

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
  • Input homework assignment grade: 100
  • Input homework assignment grade: 100
  • Input homework assignment grade: 100
  • Input homework assignment grade: 100
  • Input project grade: 100
  • Input project grade: 100
  • Input final exam grade: 100
  • Input student ID: 12
  • Input homework assignment grade: 40
  • Input homework assignment grade: 40
  • Input homework assignment grade: 40
  • Input homework assignment grade: 40
  • Input project grade: 40
  • Input project grade: 40
  • Input final exam grade: 40
  • Input student ID: 1
  • Input homework assignment grade: 20
  • Input homework assignment grade: 20
  • Input homework assignment grade: 20
  • Input homework assignment grade: 20
  • Input project grade: 20
  • Input project grade: 20
  • Input final exam grade: 20

  • Student ID: 123

  • Homework Average: 100
  • Project Average: 100
  • Final exam grade: 100
  • Final average: 100

  • Student ID: -9.25596e+061

  • Homework Average: 140
  • Project Average: 140
  • Final exam grade: 40
  • Final average: 90

  • Student ID: 1

  • Homework Average: 160
  • Project Average: 160
  • Final exam grade: 20
  • Final average: 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). 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? Thank you so much!

Your arrays like hwAverage have 10 elements (from [0] to [9]), but you refer to element [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. That causes undefined behavior.

Also, using the same for loop variable doesn't sound right. You can use i for the outer for loop and j for the inner for loops. As the code stands now, your outer for loop will never terminate since the value of i gets reset in the inner loops.

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???
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM