Is it possible to reduce my code?

Please help me reduce the code, am still a beginner in coding C++ =P
and if there is anything I can add...
It's a simple grading program.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>

using namespace std;

int main()
{
    char usr[20];
    int score;
    int retry;

    cout<<"Please enter your name.\n";
    cout<<">>. ";
    cin>>usr;
    cout<<endl;

    selection:
    cout<<"Hello "<<usr <<". Please enter your score based on 0 - 100.\n";
    cout<<">>. ";
    cin>>score;
    cout<<endl;
    cin.ignore();

    if (score <= 59) {
    cout<<"Your score is equivelant to F.\n";
    cin.get();

    cout<<"Select the number of the desired option.\n";
    cout<<"1. Try another result\n";
    cout<<"2. Exit the program\n";
    cout<<">>. ";
    cin>>retry;

    switch (retry) {
    case 1:
    goto selection;

    case 2:
    goto exit;
    }

    }

    else if (score <= 69) {
    cout<<"Your score is equivelant to D.";
        cin.get();

    cout<<"Select the number of the desired option.\n";
    cout<<"1. Try another result\n";
    cout<<"2. Exit the program\n";
    cout<<">>. ";
    cin>>retry;

    switch (retry) {
    case 1:
    goto selection;

    case 2:
    goto exit;
    }

    }

    else if (score <= 79) {
    cout<<"Your score is equivelant to C.";
        cin.get();

    cout<<"Select the number of the desired option.\n";
    cout<<"1. Try another result\n";
    cout<<"2. Exit the program\n";
    cout<<">>. ";
    cin>>retry;

    switch (retry) {
    case 1:
    goto selection;

    case 2:
    goto exit;
    }

    }

    else if (score <= 89) {
    cout<<"Your score is equivelant to B.";
        cin.get();

    cout<<"Select the number of the desired option.\n";
    cout<<"1. Try another result\n";
    cout<<"2. Exit the program\n";
    cout<<">>. ";
    cin>>retry;

    switch (retry) {
    case 1:
    goto selection;

    case 2:
    goto exit;
    }

    }

    else if (score <= 99) {
    cout<<"Your score is equivelant to A.";
        cin.get();

    cout<<"Select the number of the desired option.\n";
    cout<<"1. Try another result\n";
    cout<<"2. Exit the program\n";
    cout<<">>. ";
    cin>>retry;

    switch (retry) {
    case 1:
    goto selection;

    case 2:
    goto exit;
    }

    }

    else if (score == 100) {
    cout<<"Congratulations, You got a full mark.";
        cin.get();

    cout<<"Select the number of the desired option.\n";
    cout<<"1. Try another result\n";
    cout<<"2. Exit the program\n";
    cout<<">>. ";
    cin>>retry;

    switch (retry) {
    case 1:
    goto selection;

    case 2:
    goto exit;
    }

    }

    else {
    cout<<"The number you enterd was invalid. Do you wish to try again?\n";
    cout<<"1. Yes\n";
    cout<<"2. No\n";
    cout<<">>. ";
    cin>>retry;
    }
    cin.get();

    exit:
    return 0;
}
closed account (3pj6b7Xj)
Here is my version of your program with some modifications...
Update: 12/15/2010 :: I tested it, made some corrections and added something new, it works if you want to try it and learn something from it.
BUG: If you enter both a first name and last name in the ask name prompt, the program will crash or simply go crazy, I know how to fix this but i'll leave that part for anyone who wants the challenge. :)

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <string>

#define QUIT    3

// namespaces
using std::cout;
using std::cin;
using std::string;

/* the following class represents a student and his/her grade score. */

class Student
{
    public:
    // constructor, score is 0, we have no name
    Student() { iGradeScore = 0; bHaveName = false; }
    
    // member functions
    void AskName();
    void AskScore();
    string ScoreResult();
    int Menu();

    protected:
    string szStudentName; // the student's name
    int iGradeScore; // the student's score
    bool bHaveName; // do we have the student's name or not?
};

void Student::AskName()
/* ask for the student's name */
{
    cout << "\nPlease enter your name: ";
    cin >> szStudentName;
    bHaveName = true;
}

void Student::AskScore()
/* ask for the student's score */
{
    cout << "\n\nPlease enter your grade score (0 to 100): ";
    cin >> iGradeScore;
}

int Student::Menu()
/* main menu for program. */
{
    if (!bHaveName) return QUIT; // QUIT if no name is present.

    cout << "\n-------------------------------------------------\n";
    cout << "1. Enter score to evaluate.\n";
    cout << "2. Change your name.\n";
    cout << "3. Quit program.\n";
    cout << "-------------------------------------------------\n";
    cout << "   Active Student; " << szStudentName << "\n";
    cout << "-------------------------------------------------\n";
    cout << ":: ";
    
    int Selection;
    cin >> Selection;
    
    return Selection; // return the selection chosen by the user
}

string Student::ScoreResult()
/* look at the variable iGradeScore and return a string based on the
evaluation of that grade score. */
{
    string Evaluation;

    if (iGradeScore > 90) Evaluation = "Your score is equivalent to an A.\n\n";
    if (iGradeScore > 80 && iGradeScore < 90) Evaluation = "Your score is equivalent to a B.\n\n";
    if (iGradeScore > 70 && iGradeScore < 80) Evaluation = "Your score is equivalent to a C.\n\n";
    if (iGradeScore > 60 && iGradeScore < 70) Evaluation = "Your score is equivalent to a D.\n\n";
    if (iGradeScore < 60) Evaluation = "Oh man, you failed! Study harder!\n\n";

    return Evaluation;
}

int main()
/* main program begins here */
{
    Student A_Student; // create a student object

    cout << "Welcome to the grading program!!\n\n";

    A_Student.AskName(); // ask for the name of the student.

    /* Keep the program running until the user exits. */

    bool bQuit = false; // used to quit loop
    int MenuSelection; // used to hold menu selection chosen by user

    while (!bQuit)
    {
        MenuSelection = A_Student.Menu(); // obtain a selection from student menu
        
        if (MenuSelection == 1)
        {   // the student wants to see a score grade
            A_Student.AskScore(); // ask for the score
            cout << A_Student.ScoreResult(); // show the score grade
        }

        if (MenuSelection == 2)
        {   // student wants to change name.
            A_Student.AskName(); // ask for new name
        }

        if (MenuSelection == QUIT) bQuit = true; // quit the loop

        MenuSelection = 0; // reset before returning to top.
    }

    return 0; // end the program
}


GOTO statements are ugly, bad to use, get into the habit of writing functions or classes when you realize that you are repeating code over and over again.
Last edited on
Hi, EDIT: sorry did not know that mrfaosfx had already posted.

There are a few issues with your code so ill give you an example of how I would do it.

I had to get rid of those 'evil' goto statements {not considered good programming in c++}

As you will notice I put everything that is meant to repeat into a while loop. and have only
one copy of your menu.

Anything that you dont understand you can look up in the reference section of
this site. or if all else fails repost.

Hope this was helpful
Shredded

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

#include <iostream>

using namespace std;

int main()
{
    char usr[20];
    int score;
    int retry;
	bool	done = false;

    cout<<"Please enter your name.\n";
    cout<<">>. ";
    cin>>usr;
    cout<<endl;

    while (!done)
	{
		cout<<"Hello "<<usr <<". Please enter your score based on 0 - 100.\n";
		cout<<">>. ";
		cin>>score;
		cout<<endl;
		cin.ignore();

		if (score <= 59) 
		{
			cout<<"Your score is equivelant to F.\n";
		}
		else if (score <= 69)
		{
			cout<<"Your score is equivelant to D.";
		}
		else if (score <= 79)
		{
			cout<<"Your score is equivelant to C.";
		}
		else if (score <= 89) 
		{
			cout<<"Your score is equivelant to B.";
		}	
		else if (score <= 99) 
		{
			cout<<"Your score is equivelant to A.";
		}
		else if (score == 100) 
		{
			cout<<"Congratulations, You got a full mark.";
		}
		else if (score > 100)
		{
			cout<<"The number you enterd was invalid. Do you wish to try again?\n";
			cout<<"1. Yes\n";
			cout<<"2. No\n";
			cout<<">>. ";
			cin>>retry;

			if (retry == 1)
			{
				continue;
			}
			else if (retry == 2)
			{
				done = true;
				break;
			}
		}
		
		cin.get();

		cout<<"Select the number of the desired option.\n";
		cout<<"1. Try another result\n";
		cout<<"2. Exit the program\n";
		cout<<">>. ";
    
		cin>>retry;

		if (retry == 1)
		{
			continue;
		}
		else if (retry == 2)
		{
			done = true;
			break;
		}

	}
  
    return 0;
}
Last edited on
You guys are AWESOME!! I never thought that anyone would reply that fast..=D
best site and forum I've ever seen.

and just a question, why is it bad to use GOTO statements?
GOTO statements were put into C\C++ for people who learned to program using them in other languages. The C family has functions and logic loops so GOTO is redundent and makes for ugly code.

Lines 66 - 79 in mrfaosfx code by the way can be rewritten like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
string Student::ScoreResult()
/* look at the variable iGradeScore and return a string based on the
evaluation of that grade score. */
{
    string Evaluation;

    if (iGradeScore > 90) {Evaluation = "Your score is equivalent to an A.\n\n"; return Evaluation;}
    if (iGradeScore > 80) {Evaluation = "Your score is equivalent to a B.\n\n"; return Evaluation;}
    if (iGradeScore > 70) {Evaluation = "Your score is equivalent to a C.\n\n"; return Evaluation;}
    if (iGradeScore > 60) {Evaluation = "Your score is equivalent to a D.\n\n"; return Evaluation;}
    if (iGradeScore < 60) {Evaluation = "Oh man, you failed! Study harder!\n\n"; return Evaluation;}

} //End of string Student::ScoreResult() 


I just really wanted to point this out to show how you can use multiple return statements in a function to get rid of all of the "if and if" crap.
Last edited on
Goto is an actual jump, rather than a structure. It has scope issues that don't arise with proper loops, functions and other structures. Goto is "left-over C functionality" that was only kept in C++ to maintain support for older C code.

EDIT:
Just read Computergeek's post, and I felt like responding:
1
2
3
4
5
6
7
8
string Student::ScoreResult()
{
    if (iGradeScore > 90) return std::string("Your score is equivalent to an A.\n\n");
    if (iGradeScore > 80) return std::string("Your score is equivalent to a B.\n\n");
    if (iGradeScore > 70) return std::string("Your score is equivalent to a C.\n\n");
    if (iGradeScore >= 60) return std::string("Your score is equivalent to a D.\n\n"); // Changed to >=, since iGradeScore 60 would give no result
    if (iGradeScore < 60) return std::string("Oh man, you failed! Study harder!\n\n");
}
Last edited on
And if you wanted to go even further with that..

1
2
3
4
5
6
7
8
9
string Student::ScoreResult()
{
    std::string i = "Your score is equivalent to ";
    if(iGradeScore > 90) return std::string(i + "an A.\n\n");
    if(iGradeScore > 80) return std::string(i + "a B.\n\n");
    if(iGradeScore > 70) return std::string(i + "a C.\n\n");
    if(iGradeScore >= 60) return std::string(i + "a D.\n\n");
    if(iGradeScore < 60) return std::string("Oh man, you failed! Study harder!\n\n");
}


... but that's just unnecessary :P
1
2
3
4
5
6
char ScoreResult(int value){
  std::string evaluation = "FDCBAA"; //what happend with the 'E'?
  int index = value/10 - 5;
  if( index<0 ) index=0; //clamp
  return evaluation[index];
}

Last edited on
closed account (3pj6b7Xj)
Great example guys, lol.
1
2
3
char ScoreResult(int value){
  return std::string("FDCBAA")[(value/10 - 5)*(value>=50)];
}

Am I the only one who noticed this?
closed account (3pj6b7Xj)
yeah I noticed that but I ignored it. It works but then again, it looks confusing to anyong attemping to decipher that mess, pardon my words ... some things you can get away at the risk of confusion.
So whats that thing about the "E"?
and one more last question, if I wanted to go from one part of my code to another, how could I do that without using the GOTO statement??
closed account (3pj6b7Xj)
To put it in simple terms...you SHOULD NOT be going to another part of your program, part of WHY your using GOTO statements is because you are thinking this way. You need to think in terms of program flow and how you can write functions and classes that do the things you want done so that you dont have to use GOTO statements.

I'll give an example.

1
2
3
4
5
6
7
8
9
start:
goto askname:


askname:
cout << "what is your name"
char somename;
cin >> somename;
goto start:


that looks very ugly and I could have just written a function that does this...

1
2
3
4
void askname(char somename)
{
      cout << "what is your name";
}


and I can call this function any where in the program to ask for a name,

askname(putnamehere); or make the function return a value, etc...whatever.

C++ Primer Plus .... look for it in a book store, its a blue book and trust me, once your done reading it, it will all make sense to you in a way you've never imagined.
True, goto should be ignored, but not because of being ugly. The only issues I can see with them is that they can get very complicated very quickly, and they lack scoping rules. This means that you can get nasty errors if you try to jump from the inside of a loop to the outside, for example. Try using loops, if's, elses, switches, breaks, continues and functions to simulate the same behavior; this is the "way to go".
The only reason goto is still usable in C++, is because it was used in older C code. Since there's still a lot of C programs that used this, the statement was left for backward-compatibility.

Mrfaosfx, my code was not really meant to be a practical code, it was just there to illustrate that reducing code is not always good.
Topic archived. No new replies allowed.