Question regarding output and enum types

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

#include <iostream>
#include <string>
using namespace std;

enum grades {F = 0, D = 1, C = 2, B = 3, A = 4}courseGrade;

int assignCourseGrade(double grade)
{
  if (grade == 0)
       courseGrade = F;
  else if (grade == 1)
       courseGrade = D;
  else if (grade == 2)
       courseGrade = C;
  else if (grade == 3)
       courseGrade = B;
  else if (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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#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" ;
}
Topic archived. No new replies allowed.