run-time check failure #2 - stack around the variable was corrupted.

Hello guys, I need help on this program. The program runs fine until I put the last case in the switch statement in. If I take it out, it still works perfectly but it is not what the program require. When I put it in, I get run-time check failure#2 - stack around the variable fourthSavg. If anyone could help me out it would be greatly appreciated, thank you!


//Harris Nguyen
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

char getLG(double[],int);
string getNames(string[], int);
double getScores(double[], int);

int main()
{
int const SnA = 5, nTests=4;
string sNames[SnA];
double average[SnA];
double fSavg[nTests], sSavg[nTests], thirdSavg[nTests], fourthSavg[nTests], fifthSavg[nTests];
double sScore[4];
char letterGrade[SnA];

for (int count = 0; count < SnA; count++)
{
sNames[count]=getNames(sNames, SnA);
cout << "Enter " << sNames[count] << "'s 4 test scores: " << endl;

switch (count)
{
case 0:
fSavg[count] = getScores(sScore, count);
letterGrade[count] = getLG(fSavg, count);
cout << sNames[count] << "'s average: " << fixed << setprecision(2) << fSavg[count] << ": " << letterGrade[count] << endl << endl;
break;

case 1:
sSavg[count] = getScores(sScore, count);
letterGrade[count] = getLG(sSavg, count);
cout << sNames[count] << "'s average: " << fixed <<setprecision(2) << sSavg[count] << ": " << letterGrade[count] << endl << endl;
break;

case 2:
thirdSavg[count] = getScores(sScore, count);
letterGrade[count] = getLG(thirdSavg, count);
cout << sNames[count] << "'s average: " << fixed << setprecision(2) << thirdSavg[count] << ": " << letterGrade[count] << endl << endl;
break;

case 3:
fourthSavg[count] = getScores(sScore, count);
letterGrade[count] = getLG(fourthSavg, count);
cout << sNames[count] << "'s average: " << fixed << setprecision(2) << fourthSavg[count] << ": " << letterGrade[count] << endl << endl;
break;

case 4:
fifthSavg[count] = getScores(sScore, count);
letterGrade[count] = getLG(fifthSavg, count);
cout << sNames[count] << "'s average: " << fixed << setprecision(2) << fifthSavg[count] << ": " << letterGrade[count] << endl << endl;
break;
}
}
system("pause");
return 0;
}

char getLG(double average[], int count)
{
char letterGrade;

if (average[count] <= 100 && average[count] >= 90)
letterGrade = 'A';

else
if (average[count] <= 89 && average[count] >= 80)
letterGrade = 'B';

else
if (average[count] <= 79 && average[count] >= 70)
letterGrade = 'C';

else
if (average[count] <= 69 && average[count] >= 60)
letterGrade = 'D';

else
letterGrade = 'F';

return letterGrade;

}

string getNames(string names[], int nStudents)
{
for (int count = 0; count < nStudents; count++)
{
cout << "Enter student's name: ";
cin >> names[count];
return names[count];
}

}

double getScores(double score[], int count)
{
int sum = 0;

for (int number = 0; number < 4; number++)
{
cout << "Test " << number + 1 << ": ";
cin >> score[number];
while (score[number] < 0 || score[number] > 100)
{
cout << "Invalid score, please re-enter: ";
cin >> score[number];
}
sum += score[number];
}

int lowest = score[0];
if (score[1] < lowest)
lowest = score[1];

if (score[2] < lowest)
lowest = score[2];

if (score[3] < lowest)
lowest = score[3];

cout << "Drop lowest score: " << lowest << endl;
return ((static_cast<double>(sum)) - lowest) / 3.0;

}
Topic archived. No new replies allowed.