I'm currently trying to learn arrays, I'm trying to make a set of parallel arrays that will take a user-defined amount of total points for a class, determine the letter grade from it, then cout the results. With the code below I keep getting an endless loop of "This student made a(n): ". I also tried to ensure the calculations were working correctly by putting system("pause") within the loop after the cout statement, but then it simply stopped displaying anything at all.
Does anyone see what I'm messing up, I just know it's something stupid.
#include <iostream>
usingnamespace std;
int main()
{
int minPoints[5] = { 0 };
int totalPoints = 0;
int pointsEarned = 0;
char gradeScore[] = {'A', 'B', 'C', 'D', 'F'};
int c = 10;
cout << "Please enter the total score obtainable in the class: ";
cin >> totalPoints;
cout << "Please enter the total points earned: ";
cin >> pointsEarned;
for (int i = 0; i < 6; i++)
{
minPoints[i] = totalPoints * ((c-1) / 10);
}
for (int i = 6; i > 0; i - 1)
{
if (pointsEarned < minPoints[i])
{
cout << "This student made a(n): " << gradeScore[i];
}
}
system("pause");
return 0;
}
#include <iostream>
usingnamespace std;
int main()
{
int minPoints[5]; //0-4 values -- array goes from 0 to 4
int totalPoints;
int pointsEarned;
char gradeScore[] = {'A', 'B', 'C', 'D', 'F'}; //0-4 values -- array goes from 0 to 4
constdouble c = 10;
cout << "Please enter the total score obtainable in the class: ";
cin >> totalPoints;
cout << "Please enter the total points earned: ";
cin >> pointsEarned;
//cout << (totalPoints * ((c-1) / 10)) << endl; //test the output value
for (int i = 0; i <= 4; i++)
{
minPoints[i] = totalPoints * ((c-1) / 10);
//cout << minPoints[i] << endl; //see what's going in here
}
for (int i = 4; i >= 0; i--)
{
if (pointsEarned < minPoints[i])
{
cout << "This student made a(n): " << gradeScore[i] << endl;
}
}
return 0;
}
Alright, so looks like minPoints is storing only 0's, not really sure why. The calculation is fine according to my desk checking, so I'm going to assume it's within my method of storing in the array...
What am I doing wrong here?
EDIT: Whoops, forgot to give the runtime error message.
It's, "Run-Time Check Failure #2 - Stack around the variable 'minPoints' was corrupted."
Double edit: I figured it out, essentially I needed the variable c to be a double to prevent conversion when c/10, then I fix the second if statement to break the loop once it finds a match.