1

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.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
CProject
  • 29
  • 4
  • 9
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: **[How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver Oct 01 '15 at 14:21
  • 1) I agree with @NathanOliver. Here's a good rule of thumb for both C++ and C - if you're getting gibberish, it's a memory error in your code. Memory errors can occur from reading past the end of an array (into other memory), failing to initialize a variable before using it, or any of about a hundred other things. 2) I can appreciate the relevance of your question, since it isn't well covered in your textbook. Just be aware that many members of StackOverflow do not appreciate homework questions being posted. That said, good job on doing your own research and experimentation! That helps. :) – CodeMouse92 Oct 01 '15 at 14:28
  • In your `calculateGrade` method, where is `score` supposed to be initialised? And actually, what is that method supposed to do? Populate the `letter[]` array, or return a single grade? – atkins Oct 01 '15 at 14:29
  • I think that a debugger at this level is overkill. Think each step through, and you will find some problems pretty quickly. – daramarak Oct 01 '15 at 14:31
  • 1
    @NathanOliver -- Thank you for the link! I do have quite a bit more to learn. I'll take your article to heart and get a rubber duck and a pen and paper. JasonMc92 -- I apologize about that. My professor said we could come to this website for questions about our projects :/ score is supposed to be initialized in the getData() function. – CProject Oct 01 '15 at 14:34

3 Answers3

1

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];
Community
  • 1
  • 1
asalic
  • 664
  • 3
  • 6
  • Thanks for the tips. I still can't get the code to work but I'll figure something out. We can't use structs yet (learning about it this week) and I'm not sure about the asterisk (pass by reference? Pointers?) because he was very specific in his parameters. Appreciate the advice and help. – CProject Oct 01 '15 at 15:47
  • Son of a hooker. So it was a pass by reference? I think I figured it out. Thanks again everyone. Next time I won't ask a homework question, I'll be sure to ask a specific programming question. – CProject Oct 01 '15 at 15:56
0

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.

Rorschach
  • 734
  • 2
  • 7
  • 22
  • This answer is bad for two reasons. 1. You're not letting him learn for himself. 2. you're using an uninitialized variable. – Jonathan Mee Oct 01 '15 at 14:39
  • The uninitialized variable is his work, I only added a for loop to set values of the array. You are correct about 'not letting him learn for himself'. But, thats why I suggested him to look at his print function as its simply wrong. – Rorschach Oct 01 '15 at 14:42
0

calculateGrade has two problems:

  1. You can see from your compiler warnings. score is uninitialized
  2. It doesn't assign anything to letter[]

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[].

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288