Parallel Arrays help

A teacher uses the following table for grades:

Min Grade Max
0 F 299
300 D 349
350 C 399
400 B 449
450 A 500


Construct a program that displays the appropriate grade when the total points a student earned is entered using a parallel array (minimum and grade)
This is what I have 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* Arooj Mohammed	C++	Ch11Ex11

*/

//Ch11.cpp – displays a grade based on the total number of points
//entered by the user

#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
	//declare variables and arrays
	int total = 0;
	int minPoints[5] = {0, 300, 350, 400, 450};
	string grade[5] = {"F", "D", "C", "B", "A"};

	//get input
	cout << "Enter total points earned (negative number to end program): ";
	cin >> total; 

	//validate input
	while (total >= 0)
	{			
			int x = 0;
				while (x < 5 && total >= minPoints[x])
				{
					x = x + 1;
				}
			if (x < 5)
			{
				cout << "Grade: " << grade[x] << endl;
			}
			else 
			{
				cout << "Invalid number" << endl;
			}
			cout << "Enter total points earned (negative number to end program): ";
			cin >> total; 

	}	//end while

	return 0;
}  //end of main function 


I keep getting an incorrect output, can someone show me how to correct the program?

Enter total points earned (negative number to end program): 0
Grade: D
Enter total points earned (negative number to end program): 45
Grade: D
Enter total points earned (negative number to end program): 450
Invalid number
Enter total points earned (negative number to end program): 222
Grade: D
Enter total points earned (negative number to end program): 444
Grade: A
Enter total points earned (negative number to end program): 333
Grade: C
Enter total points earned (negative number to end program): -1
Press any key to continue . . .
On line 37, try grade[x-1] instead of grade[x].
Last edited on
You might want to use a for loop for parallel functions like so:
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
#include <iostream>
#include <string>
/*Min Grade Max
0 F 299
300 D 349
350 C 399
400 B 449
450 A 500
*/
int main()
{
   const std::string Grade[5] = {"F", "D", "C", "B", "A"}; //Grades wont be changing anytime 
   //soon, so i made it constant
   int Min[5] = {0,300,350,400,450};
   int total;
   //you want the loop to run at least once, so you would execute the code, THEN check.
   do{
       std::cout << "***Enter your grade***\n\t";
       std::cin >> total;
       if(total == -1){return 0;} //Makes the program end if user enters -1
        //For loops help with parallel arrays, and are very helpful.
        for(int x = 0; x < 5; x++){ 
            //e.g. -> IF total > 0(min[0]) AND < 300(min[1]) THEN show your grade
            if(total >= Min[x] && total <= Min[x+1]){ 
                //we can use Grade[x] because they are parallel arrays, no need for another for loop
                std::cout << "Your grade is: " << Grade[x] << std::endl;
                std::cout << "Enter another or -1 to quit\n";
                break; //Breaks from the for loop so the while loop can continue
        }
       }

   }while(total >= 0);
   return 0;
}

Please ask questions so you know what this code does, i shouldn't be just giving you the homework, but i was very bored and looking for something to do :).
Careful -

if(total >= Min[x] && total <= Min[x+1]){

If x = 4 (such as in the case for "A"), Min[x+1] becomes Min[5], which is out of bounds for the array.

That will lead to unpredictable behavior.
continuing from what I said before, you'd also have to change line 35 to if(x<6)
@ReedTompkins
how would you solve that problem? Im interested in how you would go about preventing that.
CJC0117's answers helped me make the program work, thank you all for helping me.
Last edited on
Topic archived. No new replies allowed.