/* 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???

>>'A Plus!'
grade can only hold a single character, and that is not a single character but a string. If that is what you want then change grade to a string. Note the use of the double quote instead of single quotes.

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;
}

Using a switch to break out grades will only work if grades are given only in multiples of 10. What if a student gets a 95? Does he or she really deserve an F?

Switches work by testing for an exact match, and only the values in your case labels will be matched. What you have is the equivalent of:

if( temp == 100 )
  //A+
else if( temp == 90 )
 //A
else if( temp == 80 )
 // B
//etc.....

While it's true that any switch can be written as a series of if...else if statements, the reverse is not true. Some things just have to be done with if...else if block. So, a better solution to your grading problem is in the form:

if( temp == 100 )
  //A+
else if( temp >= 90 )
 //A
else if( temp >= 80 )
 // B
//etc.....

Use a string for grade.

If you are interested in using switch-case :

char grade(int mark)
{
  int temp=mark-mark%10;  // Here we eliminate the first number .
  
  switch(temp)
  case 90:
     return 'A';
  case 80:
     return 'B';

............. and so on .

commented: 3 years too late -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.