-3

I have written a short program based on an exercise found here (Grading program), and I want to know if it's possible (out of curiosity) to replace the if statements in the .cpp with a switch/case statement. Also, additional feedback is welcome since some of the things implemented here are new to me.

Header:

// include guard
#ifndef __MAINLAUNCH_H_INCLUDED__
#define __MAINLAUNCH_H_INCLUDED__

using namespace std;

class MainLaunch
{
    int *grade;
public:
    MainLaunch();
    MainLaunch(int&);
    ~MainLaunch();
    string getLetterGrade ();
    int getGrade() {return *grade;};
};

#endif //__MAINLAUNCH_H_INCLUDED__

Cpp:

#include <iostream>
#include <string>
#include "MainLaunch.h"

using namespace std;

MainLaunch::MainLaunch()
{
    grade=new int;
    *grade=75;
}

MainLaunch::MainLaunch(int& x)
{
    grade=new int;
    *grade=x;
}

MainLaunch::~MainLaunch()
{
    delete grade;
}

string MainLaunch::getLetterGrade()
{
    int y = MainLaunch::getGrade();
    if(y==100)
        return "Perfect score!";
    else if(y>90)
        return "A";
    else if(y>80)
        return "B";
    else if(y>70)
        return "C";
    else if(y>60)
        return "D";
    else
        return "F";
}

void main ()
{
    int input;
    MainLaunch ml1;

    cout << "Hello. Please enter your grade:" << endl;
    cin >> input;

    MainLaunch ml2(input);

    cout << "Default Constructor Object Grade is " << ml1.getGrade() << "(" << ml1.    getLetterGrade() << ")." << endl;
    cout << "Declared Constructor Object Grade is " << ml2.getGrade() << "(" << ml2.getLetterGrade() << ")." << endl << endl;
    system("pause");
}
apxcode
  • 7,696
  • 7
  • 30
  • 41
IDDQD
  • 3,543
  • 8
  • 29
  • 40

2 Answers2

3
switch ((y - 1) / 10)
{
    case 9:  return y == 100 ? "Perfect score!" : "A";
    case 8:  return "B";
    case 7:  return "C";
    case 6:  return "D";
    default: return "F";
}

It'd be less ugly if the comparisons were >= - you could just:

switch (y / 10)
{
  case 10: return "Perfect score!";
  case 9:  return "A";
  case 8:  return "B";
  ...etc...

Are you sure they're not meant to be like that? 90 being an 'A' was more common in my schools than having to hit 91....

Update: looking at your link (there's an earlier step for 100 = "Perfect score!" - fine by me if you want that hanging around for the next step too)

★★ Modify the program so that it will notify the user of their letter grade

0-59 F 60-69 D 70-79 C 80-89 B 90-100 A

So y / 10 is indeed appropriate....

Community
  • 1
  • 1
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • Ah, you can pass expressions to switch! I was under the impression that you can specify variables only. – IDDQD May 09 '14 at 02:44
1

As with many things in C++ it's definitely possible to do in many ways. Sometimes it just isn't a good idea though.

This is what the switch statement would look like:

switch(y)
{
    case 100:
        return "Perfect Score!";
    break;
    case 99:
    case 98:
    case 97:
    case 96:
    case 95:
    case 94:
    case 93:
    case 92:
    case 91:
        return "A";
    break;
    case 90:
    case 89:
    case 88:
    case 87:
    case 86:
    case 85:
    case 84:
    case 83:
    case 82:
    case 81:
        return "B";
    break;
    case 80:
    case 79:
    case 78:
    case 77:
    case 76:
    case 75:
    case 74:
    case 73:
    case 72:
    case 71:
        return "C";
    break;
    case 70:
    case 69:
    case 68:
    case 67:
    case 66:
    case 65:
    case 64:
    case 63:
    case 62:
    case 61:
        return "C";
    break;
    default:
        return "F";
    break;

}

It would also restrict you to ONLY using integers since switch statements only work with them

Serdalis
  • 10,296
  • 2
  • 38
  • 58