Click to See Complete Forum and Search --> : Input int score then get grade???


chubbs1900
12-09-2007, 11:58 PM
/* This program reads a test score, calculates the letter
grade for the score, and prints the grade.
*/
#include <iostream>

using namespace std;

char studentScore (int score);

int main ()

{
cout << "Enter the test score (0-100): ";
int score;
cin >> score;
char grade = studentScore (grade);
cout << "The grade is: " << grade << endl;

cout << "\n\n\n";
system("pause");
return 0;
}

/* ===================== studentScore =====================
This function calculates the letter grade for a score.
Pre the parameter score
Post Returns the grade
*/

char studentScore (int score)
{
int temp = score / 100;
char grade;
switch (temp)
{
case 100 : grade = 'A Plus!';
break;
case 90 : grade = 'A';
break;
case 80 : grade = 'B';
break;
case 70 : grade = 'C';
break;
case 60 : grade = 'D';
break;
default : grade = 'F';
} // switch
return grade;
}

WHAT AM I DOING WRONG HERE???

watdg
12-10-2007, 12:17 AM
I think this will solve your problem. Take note that the function studentScore() will eventually return the string "A Plus!" (which is not of type char, but char*):


/* This program reads a test score, calculates the letter
grade for the score, and prints the grade.
*/
#include <iostream>

using namespace std;

char *studentScore (int score);

int main ()

{
cout << "Enter the test score (0-100): ";
int score;
cin >> score;
char *grade = studentScore (score);
cout << "The grade is: " << grade << endl;

cout << "\n\n\n";
system("pause");
return 0;
}

/* ===================== studentScore =====================
This function calculates the letter grade for a score.
Pre the parameter score
Post Returns the grade
*/

char *studentScore (int score)
{
char *grade;

if (score == 100)
grade = "A Plus!";
else if (score >= 90)
grade = "A";
else if (score >= 80)
grade = "B";
else if (score >= 70)
grade = "C";
else if (score >= 60)
grade = "D";
else
grade = "F";

return grade;
}