Grading system in C++

后端 未结 3 1979
故里飘歌
故里飘歌 2021-01-19 07:36

So this is my C++ question :

Write a program that translates a letter grade into a number grade. Letter grades are A, B, C, D and F, possibly followed by + or -. Th

相关标签:
3条回答
  • 2021-01-19 08:15

    I'd deal with each character in turn. You know that the first one should be a letter grade, so you look at that one first, and you could just use a switch statement like you've already got for that.

    uint value = 0;
    char grade_letter = grade[0];
    
    switch (grade_letter) {
        // assign value as appropriate
    }
    

    Then, if the second character exists, check if it's a + or a - and modify the value you got from the switch statement accordingly.

    if (grade.length() > 1) {
        char modifier = grade[1];
        switch (modifier) {
            case '+': value += 0.3;
            // etc.
        }
    }
    

    Before all that of course, you'll want to check for the special case of A+ and skip everything else.

    An alternative approach would be to replace the switch statements with lookups in a previously-prepared data structure, something like a std::map<char, int>, which, even if the initialisation code prepared it from hardcoded data, would open the way to loading the grade information from a configuration file or database at some point in the future. Special-casing A+ in that case becomes slightly trickier though.

    0 讨论(0)
  • 2021-01-19 08:15

    Simply adding more cases would help. Modifying your code as below:

        int main ()
        {
        String grade;
        float value;
        cout << "Enter letter grade : " ;
        cin >> grade;
    
        switch(grade)
        {
        case "A" : value = 4;
            break;
        case "A+" : value = 4.3;
            break;
        case "A-" : value = 3.7;
            break;
        case "B" : value = 3;
            break;
        case "B+" : value = 3.3;
            break;
        case "B-" : value = 2.7;
            break;        
        case "C" : value = 2;
            break;
        case "C+" : value = 2.3;
            break;
        case "C-" : value = 1.7;
            break;        
        case "D" : value = 1;
            break;
        case "D+" : value = 1.3;
            break;
        case "D-" : value = 0.7;
            break;        
        case "F" : value = 0;
            break;
        default : cout << "Wrong input " << endl;
            break;
        }
    
        cout << value;
        system("PAUSE");
        return 0;
    
        }
    

    This is a straightforward extension of what you did, there can be million other ways.

    0 讨论(0)
  • 2021-01-19 08:22

    If the input is A+ or B-, then it is not a character anymore. So, get it using a string of chars, and check the values.

    char a[3];
    float m,n;
    cin>>a;
    if(a[1]=='+')
       m=0.3;
    else if (a[1]=='-')
       m=-0.3;
    else
        m=0;
    switch(a[0])
    {
      //Assign the value of n as 4,3,2,1 depending upon the grade.
    }
    cout<<n+m;
    

    will print the grade

    0 讨论(0)
提交回复
热议问题