Hey everyone :) I'm writing a program for my class that has the user input 5 test scores and then from the inputted numbers, return the letter grade. I've begun writing the LetterGrade function, but it's not working. I'm getting a strange array of numbers/letters instead of the actual letter grade. If someone could look it over, i'd appreciate it. Thanks.

#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
float GetTestScore();
char LetterGrade(double numbergrade);

int main()
{
	float test1, test2, test3, test4, test5;

	cout << "What is the score for test 1? ";
	test1 = GetTestScore();

	cout << "What is the score for test 2? ";
	test2 = GetTestScore();

	cout << "What is the score for test 3? ";
	test3 = GetTestScore();

	cout << "What is the score for test 4? ";
	test4 = GetTestScore();

	cout << "What is the score for test 5? ";
	test5 = GetTestScore();

	cout << endl;

	cout << "Test 1 " << endl;
	cout << test1 << endl;
	cout << LetterGrade << endl;
	
	cout << "Test 2 " << endl;
	cout << test2 << endl;
	cout << LetterGrade << endl;
	
	cout << "Test 3 " << endl; 
	cout << test3 << endl;
	cout << LetterGrade << endl;

	cout << "Test 4 " << endl;
	cout << test4 << endl;
	cout << LetterGrade << endl;
	
	cout << "Test 5 " << endl;
	cout << test5 << endl;
	cout << LetterGrade << endl;
	
	cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(1);
	getch();
	return 0;
}

float GetTestScore()
{
	float TestScore;

		cin >> TestScore;

		while (TestScore < 0 || TestScore > 100)
		{
			cout << "Please enter a test score between 0 and 100. " <<endl;
			cin >> TestScore;
		}
	return TestScore;
}

char LetterGrade(double numbergrade)
{
	char LetterGrade;

	{
		if (numbergrade <= 100 || numbergrade >= 90)
			cout << "A";
		if (numbergrade <= 89 || numbergrade >= 80)
			cout << "B";
		if (numbergrade <= 79 || numbergrade >= 70)
			cout << "C";
		if (numbergrade <= 69 || numbergrade >= 60)
			cout << "D";
		if (numbergrade <= 59 || numbergrade >= 50)
			cout << "F";
	}
	return LetterGrade;
}

Recommended Answers

All 2 Replies

LetterGrade is a function. That means that if you want to use it, you have to give it an argument. So, for example, in line 31, where you wrote

cout << LetterGrade << endl;

you should have written

cout << LetterGrade(test1) << endl;

Also, I note in passing that in the function LetterGrade itself, you have defined a local variable with the same name as the function, which is legal but confusing, and you return the value of that variable without ever having given it a value, which leads to undefined behavior.

Also, there are some problems with your letterGrade function. It should look something like this :

char letterGrade(int score){
 char grade = 0;
  if(score >= 90) grade = 'A'; 
  else if(score >= 80) grade = 'B';
  else if(score >= 70) grade = 'C';
  else grade = 'F';
 return grade;
}
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.