I am supposed to convert a grade of 0,1,2,3, or 4 to F,D,C,B, or A using a function in an enumtype. I think I am close to what I am trying to get but I'm getting some errors I don't understand.
#include <iostream>
#include <string>
usingnamespace std;
enum grades {F = 0, D = 1, C = 2, B = 3, A = 4}courseGrade;
int assignCourseGrade(double grade)
{
if (grade == 0)
courseGrade = F;
elseif (grade == 1)
courseGrade = D;
elseif (grade == 2)
courseGrade = C;
elseif (grade == 3)
courseGrade = B;
elseif (grade == 4)
courseGrade = A;
else
cout << "Illegal input.\n";
switch (courseGrade)
{
case F:
cout << "F";
break;
case D:
cout << "D";
break;
case C:
cout << "C";
break;
case B:
cout << "B";
break;
case A:
cout << "A";
break;
}
}
int main()
{
double grade;
double courseGrade;
cout << "Input a number from 0 to 4 representing the grade received in the class.";
cin >> grade;
cout << endl;
cout << "The grade received is " << assignCourseGrade(grade) << ".\n";
cout << "The quality points received for the class equals " << grade << ".";
return 0;
}
Here is my ouput
"Input a number from 0 to 4 representing the grade received in the class.2
CThe grade received is 6296576.
The quality points received for the class equals 2. "
I'm getting this weird output-it says the courseGrade is C but puts it before the sentence? That is what I'm trying to get but how do I get in place of the 6296576 number. Do I need to separate the function assignCourseGrade into two functions and then execute them in int main? Thanks for any help you can provide-been trying to figure this for a couple hours.
#include <iostream>
#include <string>
enum grade { F = 0, D = 1, C = 2, B = 3, A = 4 };
grade int_to_grade( int v )
{
switch(v)
{
case 4 : return A ;
case 3 : return B ;
case 2 : return C ;
case 1 : return D ;
default : return F ;
}
}
std::string grade_to_string( grade g )
{
switch(g)
{
case A : return"A" ;
case B : return"B" ;
case C : return"C" ;
case D : return"D" ;
default : return"F" ;
}
}
int main()
{
int n ;
std::cout << "Input a number from 0 to 4 representing the grade received in the class: ";
std::cin >> n ;
const grade g = int_to_grade(n) ;
std::cout << "The grade received is " << grade_to_string(g) << ".\n" ;
}