It's not outputting what I want.

closed account (yR9wb7Xj)
I'm working on a program called Grading Program that I found in the Article section on this forum. So I need to create a program that allows the user to enter the grade scored in a programming class (o-100) that notify the user the grade letter A, B, C,D, F. I've tried reading the tutorials on here and this was my last option to ask for help, but I am stuck. If you can give an explanation on what I'm doing wrong, or any tips that will lead me in the right direction that will be great. I'm doing this because I forgot how to program, I just got back into it. Just trying to refresh my memory by reviewing an attempting to complete the Beginner Exercises that was provided in the Articles Section on here. Thanks.
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 <iostream>
using namespace std;
int main() {

	int gradeScore;


	cout << "Enter your programming grade score (0-100)" << endl;
	cin >> gradeScore;

	switch (gradeScore) {
	case 1:
		if (gradeScore >= 90 && gradeScore < 100) {
			cout << "Your grade is an" << 'A' << endl;

		}
		break;
	case 2:
		if (gradeScore >=80 && gradeScore <=89 ) {

		}
		break;
	case 3:
		if (gradeScore >= 70 && gradeScore <=79) {

		}
		break;
	case 4:
		if (gradeScore >=60 && gradeScore <=69) {

		}
		break;
	case 5:
		if (gradeScore >=0 && gradeScore<=59) {

		}
		break;

	default:
		if(gradeScore ==100)
		cout << "You got a perfect score" << endl;
	}
	return 0;
}
You seem to be mixing up the two unrelated concepts of switch-case structures and if-else structures. You do not need switch-case for this task. Also, you do not need the && or || operators for this task ;)
Just use if-else if-else for this.

LB wrote:
you do not need the && or || operators for this task ;)

Do feel free to use them if it makes your life easier, but if you would like a challenge you should try not using them.
kevinkjt2000 wrote:
Do feel free to use them if it makes your life easier, but if you would like a challenge you should try not using them.
Actually, using them in this case makes the code more error/mistake-prone. It is better to avoid them.
closed account (yR9wb7Xj)
LB & Kevinkjt2000 I just thought I needed it because it's part of the requirements for this type of program as it says here.
http://www.cplusplus.com/articles/N6vU7k9E/
. Although I would like to know the other option that kevinkjt2000 brought up. "but if you would like a challenge you should try not using them."

What do you mean LB? "You seem to be mixing up the two unrelated concepts of switch-case structures and if-else structures."

If statements when the condition statement is true it will execute. So if and else if would give another option just incase the first if statement isn't true, it would skip the condition that is false until it finds the condition statement that is true, which will execute.

Switch Statements, is when you are switching an identifier, and which ever statement in one of the cases are true will execute.

This is just for my understanding to make sure I'm in the right track. Please feel free to correct me, because that's what I want. Anyways thanks guys :)

Here's my new 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
#include <iostream>
using namespace std;
int main() {

	int gradeScore;

	cout << "Enter your programming grade score (0-100)" << endl;
	cin >> gradeScore;

	if (gradeScore >= 90 && gradeScore < 100)

		cout << "You got an A." << endl;

	else if (gradeScore >= 80 && gradeScore <= 89)
		cout << "You got a B." << endl;
	else if (gradeScore >= 70 && gradeScore <=79)
		cout << "You got a C." << endl;

	else if (gradeScore >= 60 && gradeScore <=69)
		cout << "You got a D." << endl;
	else if (gradeScore >= 0 && gradeScore <=59)
		cout << "You got a F." << endl;
	else
		cout << "You got a perfect score." << endl;
	return 0;
}
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
#include <iostream>

int main() {

    int score ;
    std::cout << "Enter your programming grade score (0-100)\n" ;
    std::cin >> score ;

    // if - else-if
    if( score < 0 || score > 100 ) std::cout << "invalid score\n" ;
    else if( score == 100 ) std::cout << "You got a perfect score.\n" ;
    else if( score > 89 ) std::cout << "You got an A.\n" ;
    else if( score > 79 ) std::cout << "You got a B.\n" ;
    else if( score > 69 ) std::cout << "You got a C.\n" ;
    else if( score > 59 ) std::cout << "You got a D.\n" ;
    else std::cout << "You got an F.\n" ; // phonetic: an F, a house, an honour, a university

    // switch-case
    if( score < 0 || score > 100 ) std::cout << "invalid score\n" ;
    else switch( score / 10 )
    {
        case 10 : std::cout << "You got a perfect score.\n" ; break ;
        case 9 : std::cout << "You got an A.\n" ; break ;
        case 8 : std::cout << "You got a B.\n" ; break ;
        case 7 : std::cout << "You got a C.\n" ; break ;
        case 6 : std::cout << "You got a D.\n" ; break ;
        default : std::cout << "You got an F.\n" ;
    }
}
closed account (yR9wb7Xj)
JLBorgesWhat I do not understand is this ----> else switch( score / 10 ) can you please explain why you did this. I'm just here trying to learn :)
Last edited on
closed account (E0p9LyTq)
The else switch replaces all the if-else-if testing.
> What I do not understand is this ----> else switch( score / 10 ) can you please explain why you did this.

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
// switch-case
if( score < 0 || score > 100 ) std::cout << "invalid score\n" ;

else // the sub-statement to be executed is the switch statement
{
    switch( score / 10 ) // switch on the value of score/10
    {
        // if( score == 100 ) then ( score/10 ) == 10
        case 10 : std::cout << "You got a perfect score.\n" ; break ;

        // if( score < 100 && ( score >= 90 ) then ( score/10 ) == 9
        case 9 : std::cout << "You got an A.\n" ; break ;

        // if( score < 90 && ( score >= 80 ) then ( score/10 ) == 8
        case 8 : std::cout << "You got a B.\n" ; break ;

        // if( score < 80 && ( score >= 70 ) then ( score/10 ) == 7
        case 7 : std::cout << "You got a C.\n" ; break ;

        // if( score < 70 && ( score >= 60 ) then ( score/10 ) == 6
        case 6 : std::cout << "You got a D.\n" ; break ;

        // if it's not any of the above, score/10 is less than 6; score is 59 or less
        default : std::cout << "You got an F.\n" ;
    }
}
closed account (yR9wb7Xj)
Thank you this actually helped alot. I understand why you did score/10 because you begin with case 10 going down from there. Which makes ten being 100 and six being 60. Also, for Else statement, that's cool that you could use a switch underneath the else statement. I always assume you could only use cout statements, and not a control structure. Good to know in the future thanks :)
> I always assume you could only use cout statements, and not a control structure.

You did use an if-statement as the sub-statement to be executed for the else.
1
2
3
4
5
6
7
8
if(condition) { /* something */ }

// else if( condition2 ) { /* something else */ }

else // the sub-statement to be executed is another if-statement
{
     if( condition2 ) { /* something else */ }
}


I used a switch-statement instead of another if-statement.
closed account (yR9wb7Xj)
Thanks again JLBorges it make sense now. I guess I need to review control structures again on the tutorial section to make sure I deeply understand it.
Last edited on
Topic archived. No new replies allowed.