Hello. I'm currently working on a C++ assignment where I need to calculate a student's GPA from a text file containing 4 grades and the corresponding credit values for the class.

I know how to write the program to get the correct GPA, but I am having problems figuring out a way to convert the letter values (A, B, C, D) into their numeric values (4, 3, 2, 1). We are not allowed to use a selection statement (if , switch) and have to do this by converting the letters into their ASCII value (by going from a char to an int).

I feel like I should know this, but I've been scratching my brain over how to logically code it.

ch int qpts (quality points)
A -> 65 -> 4
B -> 66 -> 3
C -> 67 -> 2
D -> 68 -> 1

So basically, changing char A to int A converts it into it's ASCII value of 65, and etc for the other letters. The part I haven't been able to figure out is how to change the ASCII value into the quality points value. So I need code to make 65 = 4, 66 = 3, etc. Just pointing me in the right direction would be fine. I have the rest of the program figured out.

Also, don't give me any advanced code for solving this. We're only on chapter 4 and this is an entry-level C++ class, which is why I'm bothered than I'm having trouble with this. Thank you.

Recommended Answers

All 13 Replies

65 - (61 + 0) = 4
66 - (61 + 2) = 3
67 - (61 + 4) = 2
68 - (61 + 6) = 1

Right, I understand that. It's also obvious how I could use that with an if or a switch statement. What I don't know how to do is make a expression that can take any given ASCII value (65 through 68) and match it with the corresponding quality point value (4 through 1). The expression needs to be able to accept any of those ASCII values and result in the correct quality point value.

So, if it reads 66 (after I've converted B from a char to an int), what code would I need to have it determine that the quality point value is 3? And the same for 67 being 2, etc.

Boy, I would have done this with an enum, but it sounds like you can't. Can you use a switch?

switch (asciiGrade) {
 case 65:
   qpts = 4;
   break;
 case 66:
   qpts = 3;
   break;
 /* etc... */
}

It works just like an if ... else, but it's not actually one. In a typical programming course, you're at about the right point to be learning these. It's not unusual to add the comment "you may not use an if ... else" to assignments in these lessons.

He said no. Also note we're on chapter 4 of a entry-level C++ class. I know how to do switches, but we actually haven't covered them in class yet. Same for loops. We've done if ... else though, of course. But he said explicitly that we cannot use them for this! It's funny cause this class has been so easy up until now.

I guess I can use four separate expressions, one for each grade. So four of the same expression since it has to be able to accept any value.

Here's psuedo code.

cast A to int qa
cast B to int qb
cast C to int qc
cast D to int qd
qa = qa - (61 + 0)
qb = qb - (61 + 2)
qc = qc - (61 + 4)
qd = qd - (61 + 6)

What is the name of the chapter? Perhaps that will provide a clue.

What I need is an statement that does:

qa = qa - (61 + 0)
qb = qb - (61 + 2)
qc = qc - (61 + 4)
qd = qd - (61 + 6)

But the statement has to be able to do this based on the letter input. Here's what's actually happening in the program.

I have a text file with the following content:

Lonesome Polecat
ABBC
4323

1st line: firstname, lastname
2nd line: g1, g2, g3, g4
3rd line: credit1, credit2, credit3, credit4

So I get the name, the four grades he made, and the credits the class is worth. I don't need help with any of this part. I have all the getfile figured out already and have a program that functions.

The program reads the grades one by one, converts them from char to int, then uses the ASCII value of each letter (A = 65, etc) and somehow changes it to it's quality point equivalent (A(65)=4, B(66)=3, C(67)=2, D(68)=1). That's what I need to figure out. From there I just calculate the GPA by multiplying quality points by credit and dividing the result by credit.

Maybe now I've made what I'm trying to accomplish more clear.

Edit: I need to stop interchanging expression and statement. I have it right now I believe.

This should work

int k = 0;
int qA = static_cast<int>('A') - (61 + k);
k+=2;
int qB = static_cast<int>('B') - (61 + k);
k+=2;
int qC = static_cast<int>('C') - (61 + k);
k+=2;
int qD = static_cast<int>('D') - (61 + k);

I'm not seeing how that is going to do what I need. qA through qD is each grade? Corresponding to the text file, qA = 4, qB = 3, qC = 2, and qD = 3?

I'm not seeing how that is going to do what I need. qA through qD is each grade? Corresponding to the text file, qA = 4, qB = 3, qC = 2, and qD = 3?

qD will be 1, not 3.

the static_cast<int>('A') is the letter grade. You can replace the ('A') with whatever char variable you assigned the grade letter A to.

Generally, E is a failing grade, its value is 69. What if you used E as a "baseline" for your QP? You would then subtract the earned grade from this "baseline" to generate a QP value:

char letterGrade = '\0';
cout << "Enter letter grade: ";
cin >> letterGrade;
qp = 'E' - letterGrade;

If you enter an 'A' it would translate to:

qp = 'E' - 'A'; //becomes --v
qp = 69 - 65;   //becomes --v
qp = 4;

a 'B' would translate to:

qp = 'E' - 'B'; //becomes --v
qp = 69 - 66;   //becomes --v
qp = 3;

etc...

Generally, E is a failing grade, its value is 69. What if you used E as a baseline for your QP? You would then subtract the earned grade from this "baseline" to generate a QP value:

char letterGrade = '\0';
cout << "Enter letter grade: ";
cin >> letterGrade;
qp = 'E' - letterGrade;

oooo clever ... :)

I didn't even think of that one.

ROFL yea that's what I need. I knew it was going to be simple. Simplicity can be so difficult. Thanks everyone!

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.