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:
/* 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 . . .
#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 :).