Reputation: 29
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.
Upvotes: 1
Views: 1830
Reputation: 664
Try the following method instead of yours:
// function definition for calculateGrade()
void calculateGrade(const int NO_OF_STUDENTS, int *testScores, char *letter)
{
for(int i = 0; i < NO_OF_STUDENTS; i++)
{
if(testScores[i] > 89)
{
letter[i] = 'A';
}
else if(score > 79)
{
letter[i] = 'B';
}
else if(score > 69)
{
letter[i] = 'C';
}
else if(score > 59)
{
letter[i] = 'D';
}
else // As you are a beginner, try to always cover all the possibilities in an if chain, you'll thank me later
printf("Please give a score greater than 59 for student number %d", i);
}
and call like this:
calculateGrade(NO_OF_STUDENTS, testScores, letterGrades);
As it is homework I'll let you discover the meaning of the asterisk and why I don't return a value.
And a final advice, maybe for a later moment when you have a better grasp of the language, try to group the fields in a class or struct (almost the same in C++, check this What are the differences between struct and class in C++? for the diff) and instead of array of first, last names and scores you'll end up with something like:
struct Student
{
string fName;
string lName;
int testScore;
}
Student students[NO_OF_STUDENTS];
Upvotes: 1
Reputation: 38919
calculateGrade
has two problems:
score
is uninitializedletter[]
The concept of your program is that each index represents a student. So what you want is to take in the score of an index (student) and assign it to that index's (student's) letter
.
To do that we need to take in a score and a letter. So your definition should look like: void calculateGrade(int[], char[]);
. Internally rather than returning the grade letter you'd assign it to the char[]
.
Upvotes: 0
Reputation: 764
This code is not very well written, but, as a quick fix, you might just initialize your char LetterGrades[]
, just as you have initialized 3 other arrays. So, you could do>
char calculateGrade(char letter[])
{
int score;
char gradeLetter[5] = {'A', 'B', 'C', 'D', 'F'};
for(int i=0; i<5; i++) {
letter[i] = gradeLetter[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];
//}
}
Output is then OK, it prints out 'A', 'B', 'C' etc.
One more thing, your printResult
function is poorly written. It doesn't do anything but just list out all of the arrays (test and you'll see that score 59 gets an 'A' and score 100 gets 'C'). You have to make code so that scores and grades are corresponding with people who earned them.
Upvotes: 0