simplifying code and looping

Is there a better way to do this than adding output = GRADE + gradeOut; between each if statement? Also how can I loop it back to the beginning if the score is invalid?

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
#include <iostream>
#include <string>
using namespace std;

const string SCORE = "Enter score (0-100): ";
const string GRADE = "Your grade: ";

int main()
{
	int scoreIn;
	string gradeOut;
	string output;

	cout << SCORE;
	cin >> scoreIn;

	system("cls");

	if (scoreIn >= 0 && scoreIn <= 59){
		gradeOut = "F";
		output = GRADE + gradeOut;
		cout << output << endl;
		cout << "Unacceptable." << endl;
	}
	else if (scoreIn >= 60 && scoreIn <= 69){
		gradeOut = "D";
		output = GRADE + gradeOut;
		cout << output << endl;
		cout << "Study harder." << endl;
	}
	else if (scoreIn >= 70 && scoreIn <= 79){
		gradeOut = "C";
		output = GRADE + gradeOut;
		cout << output << endl;
		cout << "Not bad." << endl;
	}
	else if (scoreIn >= 80 && scoreIn <= 89){
		gradeOut = "B";
		output = GRADE + gradeOut;
		cout << output << endl;
		cout << "Good job!" << endl;
	}
	else if (scoreIn >= 90 && scoreIn <= 99){
		gradeOut = "A";
		output = GRADE + gradeOut;
		cout << output << endl;
		cout << "Awesome!" << endl;
	}
	else if (scoreIn == 100){
		gradeOut = "A";
		output = GRADE + gradeOut;
		cout << output << endl;
		cout << "Perfect score!" << endl;
	}
	else{
		cout << "Invalid score. Please re-enter data." << endl;
	}

	system("pause");
	return 0;
}
Last edited on
== is for comparison and = is for assignment. So lines 20 , 25 , 30 , 35 , 40 , and 45 should be =

Also on line 12 you are assigning the output when gradOut is undefined. You should get gradeOut first.

Last thing to mention when you do else if that means it has to check the previous ones first. So you could do if score is less than equal 59...else if score is lessthan or equal to 69...ect.
Awesome! Thank you very much! I did what you said about changing == to = and then I changed string output = GRADE + gradeOut; to just string output; and then added output = GRADE + gradeOut; to each if statement. Now it works :)
is there an easier way than adding output = GRADE + gradeOut; to each if statement?
also is there a way to loop the code back to the start if the score is invalid? because right now it just closes and i have to reopen it.
is there an easier way than adding output = GRADE + gradeOut; to each if statement?


You can simply change cout << output << endl; to cout << GRADE << gradeOut << endl; Then you would not need the output string.

also is there a way to loop the code back to the start if the score is invalid?

You can use a do/while[1] loop for that. Basically just wrap the stuff you want to loop with it.

1
2
3
4
5
do
{

    //stuff
} while( condition_is_true );


[1]http://www.cplusplus.com/doc/tutorial/control/
Great, thanks again! I added a do/while loop that says:

1
2
3
4
5
6
do
{

//stuff

} while (scoreIn <= 0 || scoreIn >= 100);


It works perfectly!
Topic archived. No new replies allowed.