Pages

Sunday, September 01, 2013

Write a Program of Student Grading in C++

This program reads a test score, calculates the letter grade for the score, print the grade

Source Code:


 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.h>
   #include <conio.h>
   
   char scoreToGrade (int score);
   
   int main ()
      {
      cout << " Enter the test score (0-100): ";
      int score;
      cin  >> score;
      
     char grade = scoreToGrade (score);
      cout << " The grade is: " << grade << endl;
      getch();
      return 0;
             } //main
   /* ============= scoreToGrade ===================
   This function calculates the letter grade for a score
   Pre  the parameter score
   Post Return the grade                     */
   char scoreToGrade (int score)
   {
     int temp = score / 10;
   char grade;
   switch (temp)
     {     
       case 10 :
       case  9 : grade = 'A';
                break; 
       case  8 : grade = 'B';
                break; 
       case  7 : grade = 'C';
                break; 
       case  6 : grade = 'D';
                break; 
       default : grade = 'f';
                break; 
                }// switch
    getch();
    return grade;    
                      }// scoreToGrade

Result:


 Enter the test score (0-100): 89
 The grade is: B

No comments:

Post a Comment