Arrays issue. won't break out of loop.

Nov 11, 2013 at 4:12pm
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  #include <iostream>
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;
}

Nov 11, 2013 at 4:34pm
Line 24: should be i-- instead of i - 1 :)
Nov 11, 2013 at 8:16pm
Which didn't really help with the issue at all. I thought i-- and i - 1 were interchangeable, anyway?
Nov 11, 2013 at 8:19pm
Line 24: should be i -= 1 instead of i - 1
Nov 11, 2013 at 8:22pm
Neither of which fix the issue.
Nov 11, 2013 at 8:33pm
This is what I got so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <iostream>
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 Nov 11, 2013 at 8:33pm
Nov 12, 2013 at 1:17pm
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?
Nov 12, 2013 at 1:58pm
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.
Nov 13, 2013 at 1:13pm
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 Nov 13, 2013 at 4:04pm
Topic archived. No new replies allowed.