I am at a loss. I have tried several things and have 90% of the program working. In fact, it compiles and runs fine. My output is very bizarre characters where the letter grade is supposed to be.
Our textbook does not offer examples with things like this and it is very difficult to search for. I need to return a letter grade in one function and use it in a table later on in another function. How do you do this?
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// function prototypes
void getData(string [], string [], int []);
char calculateGrade(char []);
void printResult(string [], string [], int [], char [], int);
int main()
{
// define 4 parallel arrays
const int NO_OF_STUDENTS = 5;
string studentFNames[NO_OF_STUDENTS];
string studentLNames[NO_OF_STUDENTS];
int testScores[NO_OF_STUDENTS];
char letterGrades[NO_OF_STUDENTS];
// call getData() to populate three of the four parallel arrays
getData(studentFNames, studentLNames, testScores);
// call calculateGrade() to provide values for the fourth parallel array
calculateGrade(letterGrades);
// call printResult() to display report form the parralel arrays
printResult(studentFNames, studentLNames, testScores, letterGrades, NO_OF_STUDENTS);
return 0;
}
// function definition getData()
void getData(string fName[], string lName[], int scores[])
{
// the follow arrays are used for test data (do not modify)
string fNameTest[5] = {"Humpty", "Jack", "Mary", "Jack", "King"};
string lNameTest[5] = {"Dumpty", "Horner", "Lamb", "Sprat", "Cole"};
int scoresTest[5] = {59, 88, 100, 75, 60};
// use a suitable loop to populate the appropriate "empty" arrays
// with values from the three initialized test arrays
for(int index = 0; index < 5; index++)
{
fName[index] = fNameTest[index];
lName[index] = lNameTest[index];
scores[index] = scoresTest[index];
}
}
// function definition for calculateGrade()
char calculateGrade(char letter[])
{
int score;
char gradeLetter[5] = {'A', 'B', 'C', 'D', 'F'};
//for(int i = 0; i < 5; i++)
//{
if(score > 89)
{
return gradeLetter[0];
}
if(score > 79)
{
return gradeLetter[1];
}
if(score > 69)
{
return gradeLetter[2];
}
if(score > 59)
{
return gradeLetter[3];
}
return gradeLetter[4];
//}
}
// function definition for printResults()
void printResult(string lName[], string fName[], int score[], char letter[], int size)
{
cout << setw(15) << left << "Student Name" << setw(9) << right << "Test Score" << " " << setw(5) << "Grade" << endl << endl;
for(int index = 0; index < size; index++)
{
cout << setw(15) << left << (lName[index] + ", " + fName[index]);
cout << setw(9) << right << score[index] << " " << setw(5) << letter[index] << endl;
}
}
Here is the program. Keep in mind that I have to use only the three functions and I cannot change any of the constants or local variables. I suspect later we are going to modify this program later to read from a file but that is not the issue as of now.
I have tried a for loop with the if/else if/else statements and that gives spades, diamonds, and w's. I have tried using arrays for gradeLetter and testScores and I still get gibberish in return.
Any help would be greatly appreciated. Sorry if something similar has been done. It's a nightmare searching for something like this.