### Arrays issue. won't break out of loop.

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.

 ``12345678910111213141516171819202122232425262728293031323334353637`` `````` #include using namespace 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; } ``````
Line 24: should be `i--` instead of `i - 1` :)
Which didn't really help with the issue at all. I thought i-- and i - 1 were interchangeable, anyway?
Line 24: should be `i -= 1` instead of `i - 1`
Neither of which fix the issue.
This is what I got so far:

 ``1234567891011121314151617181920212223242526272829303132333435`` `````` #include using namespace 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 const double 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; } ``````
Last edited on
The output for the totalPoints * ((c--) / 10) is coming back as 0 when I tested it, not sure why.

I'm also now getting run time errors, any suggestions on how to fix this?
 I thought i-- and i - 1 were interchangeable, anyway?

No. `--i;` and `i = i - 1;` are interchangeable, and both are interchangeable with `i -= 1;`.

`i--;` is almost the same, but evaluates to the value of i before the decrement, not after it.

`i - 1` on it's own as a statement does nothing.
Gotcha. Useful to know, thanks.

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.
Last edited on
Topic archived. No new replies allowed.