Using If-If only statement

Just a hitch up. I've been doing this already but can't seem to make it run and sorry I haven't replied to my first topic already. Been busy on some other stuff. I might dig it back when I have time. For the meantime, I am creating a C++ program that will accept the final grade and display its equivalent based on the below table

Grade Equivalent
0.0 -> 59.4 0.00 F
59.5 -> 64.4 1.00 D
64.5 -> 70.4 1.50 D+
70.5 -> 76.4 2.00 C
76.5 -> 82.4 2.50 C+
82.5 -> 88.4 3.00 B
88.5 -> 94.4 3.50 B+
94.5 -> 100.0 4.00 A

And here's my 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
42
43
44
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	double grade, numgrade;
	char letgrade, y;
	char A;
	char B;
	char B+;
	char C;
	char C+;
	char D;
	char D+;
	char F;

	cout << "\t--------------Welcome to the grade calculator----------------\n" << endl;
	cout << "Enter your final grade in numbers: ";
	cin >> grade;                //grade expressed in numbers

	if (grade >= 94.5 && grade <= 100)
	{
		if (grade >= 88.5 && grade <= 94.4)
		{
			if (grade >= 82.5 && grade <= 88.4)
			{
				if (grade >= 76.5 && grade <= 82.4)
				{
					if (grade >= 70.5 && grade <= 76.4)
					{
						if (grade >= 64.5 && grade <= 70.4)
						{
							if (grade >= 59.5 && grade <= 64.4)
							{
								cout << "You got a numerical grade of " << grade << "and a letter grade of" << letgrade << endl;
								cout << "You did a remarkable job!!" << endl;
							}
							else
								cout << "You have shamed yourself and your parents!";
			}
						system("pause");
	return 0;
}


I know I'm still a noobish here by the way and I'm an Info tech student btw.
In this code there are many problems: the if one after the other don't make a lot of sense. Also the variables char A, B, .... Those should not be variables, but the values of your table.

This is a possible solution:

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
42
43
#include <iostream>
#include <vector>
#include <string>
#include <utility>

using namespace std;

int main()
{

    double grade = 0.0, num_grade = 0.0;
    string letter_grade;
    int segment = 0;

    vector<pair<double, string> > table;
    table.push_back(make_pair(0.0, "F"));
    table.push_back(make_pair(1.0, "D"));
    table.push_back(make_pair(1.5, "D+"));
    table.push_back(make_pair(2.0, "C"));
    table.push_back(make_pair(2.5, "C+"));
    table.push_back(make_pair(3.0, "B"));
    table.push_back(make_pair(3.5, "B+"));
    table.push_back(make_pair(4.0, "A"));

    cout << "Enter your final grade in numbers: ";
    cin >> grade;

    if (grade >= 94.5 && grade <= 100) segment = 7;
    else if (grade >= 88.5 && grade <= 94.4) segment = 6;
    else if (grade >= 82.5 && grade <= 88.4) segment = 5;
    else if (grade >= 76.5 && grade <= 82.4) segment = 4;
    else if (grade >= 70.5 && grade <= 76.4) segment = 3;
    else if (grade >= 64.5 && grade <= 70.4) segment = 2;
    else if (grade >= 59.5 && grade <= 64.4) segment = 1;
    else segment = 0;

    num_grade = get<0>(table[segment]);
    letter_grade = get<1>(table[segment]);

    cout << "Your grade is " << num_grade << " and your letter grade is " << letter_grade << endl;

    return 0;
}
Last edited on
EDIT: You could also use iterators

1
2
3
4
5
6
7
    vector<pair<double, string> >::iterator it = table.begin();
    it += segment;

    num_grade = it->first;
    letter_grade = it->second;

    cout << "Your grade is " << num_grade << " and your letter grade is " << letter_grade << endl;
Last edited on
Hi

Welcome to cplusplus :+D and seasons greetings while we are at it !!

First up, about how to post a question. So actually ask a question to start with. Specify what your problem is, and where you think it might be going wrong. If your code doesn't compile (yours doesn't), post the compiler errors in full and make sure the line numbers in the errors match the code you posted.

OK, just some stuff to realise about your code.

With nested if statements, the inner part only executes if the outer part is true. For example, line 24 executes only if line 22 is true and so on. So the code doesn't do what you want. Investigate the use of an else if statement.

Also, when an if statement evaluates to false, execution continues after the compound statement that follows the the if statement ( that is, what is executed if true part). So if line 22 evaluates to false, then execution jumps to line 42. With that, you don't have your closing braces - these must match up. One can set their IDE to do this automatically. The lack of braces should have generated lots of compiler errors.

Now for the biggest problem often encountered by beginners: initialisation of variables. What is the value of letgrade on line 36? Or at any stage for that matter? Always initialise your variables to something, preferably at declaration. For letgrade , I would have initialised it to 'Z' , that way if you see that in the output, you might realise that you haven't set it anywhere.

With the variables B, B+, just wondering what you are going to store in those. Do you realise that a char only holds a single character? Could your get away without those variables?

There we go, some stuff to work on :+) Hope it all helps a bit.
@minomic

Hi,

I don't think the OP is at the stage of the course that they have been taught about vectors pairs or iterators - so simple if statements is probably all they are after. But be careful not to do the homework for them.

Any way, regards & seasons greeting to you as well :+D Cheers


Edit:

@JemCel03

Also, this post belongs in the beginners section, you can edit your post and move it there.
Last edited on
@TheIdeasMan

You're right: I'll keep that in mind.

Seasons greetings to you as well!
Topic archived. No new replies allowed.