0

I'm getting the following error in Visual Studio that I don't understand:

main.cpp(21): error C2440: '=': cannot convert from 'void' to 'char'
main.cpp(21): note: Expressions of type void cannot be converted to other types

This is my code:

void getLetterGrade(double totalAvg, char& letter_Grade)
{
    if (totalAvg >= 90) {
        letter_Grade = 'A';
    }
    else if (totalAvg < 90 && totalAvg >= 80) {
        letter_Grade = 'B';
    }
    else if (totalAvg < 80 && totalAvg >= 70) {
        letter_Grade = 'C';
    }
    else if (totalAvg < 70 && totalAvg >= 60) {
        letter_Grade = 'F';
    }
}

int main()
{
    double totalGrade = 74;
    char letterGRADE;
    letterGRADE = getLetterGrade(totalGrade, letterGRADE);
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Baker
  • 1
  • Please provide a [mre], along with a description of the symptoms you observe, sample input to trigger your observation, the observed output in contrast to the output you expect. – Yunnosch Apr 11 '22 at 06:05
  • First of all your condition doesn't need the `totalAvg < 90 &&` (etc.) part. That's kind of implied by the conditions. Secondly, what happens if `totalAvg < 60`? You don't have any plain `else`. – Some programmer dude Apr 11 '22 at 06:06
  • 2
    What is the problem? Please show a [mre] with the full error message – Alan Birtles Apr 11 '22 at 06:06
  • 1
    Just use `getLetterGrade(totalGrade, letterGRADE);` you are passing `letterGRADE` by reference and returning void so you can't/don't need to assign the result to `letterGRADE` – Alan Birtles Apr 11 '22 at 06:08
  • The function should return a value, not store it in a reference argument. – molbdnilo Apr 11 '22 at 06:09
  • 2
    Get yourself a [decent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Paul Sanders Apr 11 '22 at 06:10

1 Answers1

4

Your function getLetterGrade returns void, that means "nothing". Therefore it is illegal to use it on the right hand side of an assignment.

This here

letterGRADE = getLetterGrade(totalGrade, letterGRADE);

will result in an error, because it tries to use the return value of the function, but there is none.

But, since you pass the second parameter by reference (by saying "char & letter_Grade") you already get the required letter back. Just change your code to

getLetterGrade(totalGrade, letterGRADE);

and you should be fine. The value of the variable letterGRADE will already have been updated when the called function returns.

As an alternative, you could change the function to return the letter instead:

char getLetterGrade(double totalAvg)
{
    char letter_Grade = '?';
    if (totalAvg >= 90)
    {
        letter_Grade = 'A';
    }
    else if (totalAvg < 90 && totalAvg >= 80)
    {
        letter_Grade = 'B';
    }
    else if (totalAvg < 80 && totalAvg >= 70)
    {
        letter_Grade = 'C';
    }
    else if (totalAvg < 70)
    {
        // Anything below 70 is flunked, so no need to give an upper limit (otherwise an average of 50 would not get a mark at all)
        letter_Grade = 'F';
    }
    return letter_Grade;
}

Now you would call it like so, using a return value:

letterGRADE = getLetterGrade(totalGrade);
PMF
  • 14,535
  • 3
  • 23
  • 49