//This program reads a course score and prints the
//associated course grade

#include <iostream>
using namespace std;


void getScore(int& score);
void printGrade(int score);

int main()
{
	int courseScore;

	cout << "Line 1: Based on the course score, \n"
		<< "   this program computes the "
		<< "course grade." << endl;

	getScore(courseScore);

	printGrade(courseScore);

	return 0;
}

void getScore(int& score)
{
	cout << "Line 4: Enter course: ";
		cin >> score;
	cout << endl << "Line 6: Course score is "
		<< score << endl;
}

void char printGrade(int cScore)
{

	char gr = " ";

	if (cScore >= 90)

		gr = "A";
	
	else if (cScore >= 80)
		gr = "B";

	else if (cScore >= 70)
		gr = "C";

	else if (cScore >= 60)
		gr = "D";
	else 
		gr = "F";
	return gr;
}

Every time I try to compile this I get errors. Please help.

Recommended Answers

All 7 Replies

what errors? I can not see your monitor.

what errors? I can not see your monitor.

ha ha ha true , learn to read the errors and dig them , that is C++ life.
Think about places where we don't see a error . Fortunately the compiles
stops you by showing you a error.

Your problem is with the printGrade function. You declare it with a void return type but define it with void char. You then call it without accepting a return value but actually return a value.

You have compile-time and logic errors.

And you're using double-quotes for character constants. You should be using single-quotes.

Also this char gr = " "; and all the similar statements assigning a string to the char variable, such as gr = "F"; will cause errors, as you can't assign multiple characters to a single char. A string (that which is enclosed in double quotes) has an implied newline character as well as what you see.

First, the function's name is printGrade, so that's what it ought to do. Just do the output right then and there. Otherwise, call it convertGrade or something similar.

If you do want to return the letter equivalent of the numeric input, it must have a return type of char. Just char. Store just a single char to the variable, as in gr = 'A'; and then return that variable at the end. Or simply return the appropriate value as the action of each condition. As adam1122 mentioned, you will need to capture or output the returned value in main( ).

Poor boy!

You wrote improper code - check declaration & definition of printGrade function.

void printGrade(int score);
|-------- here is a problem. remove void at declaration and definition.
V
void char printGrade(int cScore)
{

char gr = " ";

if (cScore >= 90)

gr = "A";

else if (cScore >= 80)
gr = "B";

else if (cScore >= 70)
gr = "C";

else if (cScore >= 60)
gr = "D";
else
gr = "F";
return gr;
}

//This program reads a course score and prints the
//associated course grade

#include <iostream>
using namespace std;


void getScore(int& score);
void printGrade(int score);

int main()
{
	int courseScore;

	cout << "Line 1: Based on the course score, \n"
		<< "   this program computes the "
		<< "course grade." << endl;

	getScore(courseScore);

	printGrade(courseScore);

	return 0;
}

void getScore(int& score)
{
	cout << "Line 4: Enter course: ";
		cin >> score;
	cout << endl << "Line 6: Course score is "
		<< score << endl;
}

void char printGrade(int cScore)
{

	char gr = " ";

	if (cScore >= 90)

		gr = "A";
	
	else if (cScore >= 80)
		gr = "B";

	else if (cScore >= 70)
		gr = "C";

	else if (cScore >= 60)
		gr = "D";
	else 
		gr = "F";
	return gr;
}

Every time I try to compile this I get errors. Please help.

To the OP:
Read the vmanes post. It is the most relevant here. Technically, you are correct in defining printGrade as void. But as other pointed, the defination should have been also of a void return type as you function names suggests that it should PRINT a value rather than RETURNing it. So switch to void and change the return statement at last to a print statement( in C++, the cout).

Poor boy!

The boy is less poor than you are, he submitted his code in the code tags at least.

ha ha ha true , learn to read the errors and dig them , that is C++ life.

If you were saying all this to Ancient Dragon, alas you didn't got his point. He was emphasizing that the OP should be specific in what error he is getting. And one should respect the quality posts Ancient delivers.

commented: Nice post ! +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.