Mike
Mike

Reputation: 11

What does it mean if a "variable or field" is declared void? Error message help

variable or field `letterGrade' declared void. This error message is coming up on the last iteration of the function 'letterGrade'. Anyone have an idea why?

#include <string>
#include <iostream>
using namespace std;
void letterGrade (int score, string& scoreLetter);
string scoreLetter;
int main()
{
    int score;
    char A, B, C, D, F;
    cout<<"Enter the grade: ";
    cin>> score;
    letterGrade (score, scoreLetter);
    cout<<"The letter grade is a(n) "<< scoreLetter<<".";
    system ("pause");
    return 0;
}
void letterGrade (score, scoreLetter)
{
     for (score >= 90)
     {
             scoreLetter = 'A';}
      if (score == 100)
     {
             scoreLetter.insert (1, "+");
     } 
     else if (8<=score% 10 && score% 10 <= 9)
     {
                   scoreLetter.insert (1, "+");
     else if (0<=score% 10 && score% 10 <=1)
     {
                   scoreLetter.insert (1, "-");
     } 

Upvotes: 0

Views: 8718

Answers (5)

3v0
3v0

Reputation: 1

IIRC for can be written with only the comparison, aka end condition, specified

for (a>b)
for (a=2; a>b)
for (a>b; a++)

Should all work providing we do the missing initialization or iterator elsewhere in the code.

Upvotes: 0

Thomas Matthews
Thomas Matthews

Reputation: 57728

Are you actually looping in the letterGrade function?

Most homework assignments like this only require an if-then-elseif-else ladder.

I suggest removing the for statement and replacing with an if statement. If you decide you need to loop or repeat, restore the for statement.

A for loop would allow you to traverse a table of scores vs. grade strings:

struct Grade_Score
{
    unsigned int grade;
    const char * grade_text;
};

const Grade_Score grade_table[] =
{
    {100, "A+"},
    {90, "A"},
    {80, "B"},
    {70, "C"},
    {60, "D"}
};
const unsigned int NUM_GRADE_ENTRIES =
    sizeof(grade_table) / sizeof(grade_table[0]);

std::string Grade_To_Score(unsigned int grade)
{
    std::string score = "F";
    for (i = 0; i < NUM_GRADE_ENTRIES; ++i)
    {
        if (grade >= grade_table[i].grade)
        {
             score = grade_table[i].grade_text;
             break;
        }
    }
    return score;
}

A similar search can be performed using the std::lower_bound or std::upper_bound functions.

Edit 1:
I suggest you replace system("pause"); with something more portable like:

cout << "Press Enter to continue\n";
cout.ignore(10000, '\n');

Not all platforms support the pause command.

Upvotes: 0

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361612

void letterGrade (score, scoreLetter)
{
   //...

In the function definition above, you forgot to specify the types. Mention the types as:

void letterGrade (int score, std::string & scoreLetter)
{             //  ^^^this    ^^^^^^^^^^^^^this
   //...

Don't forget to #include<string>.


Another problem is this:

 for (score >= 90)

The form of for should be this:

 for(initialization; condition ; increment/decrement/changing-some-value)

Example:

 for ( int i = 0 ;  i <= score ; i++) 

Upvotes: 1

Kiril Kirov
Kiril Kirov

Reputation: 38173

First of all, as the others said - fix the letterGrade definition:

                  vvv       vvvvvvvvvvvv
void letterGrade( int score, std::string& scoreLetter )

After that, fix the for, it should be something like:

 for (; score >= 90; --score )
 {
  //..
 }

Also, note that you shadow the global scoreLetter in letterGrade

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 190976

You aren't naming the types on the parameters of the definition of letterGrade.

Upvotes: 1

Related Questions