im trying to chang from if else if to while statement

I did this average project using the "if else if" statement is it possible to change from "if else if" to "while loop".

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

int main()
{
	int num1,num2,num3,num4, Average;

	cout<<"Enter your grade in the following units."<<endl;
	cout<<"COT 211=";
	cin>>num1;
	
	cout<<"COT 212=";
	cin>>num2;
	
	cout<<"COT 213=";
	cin>>num3;
	
	cout<<"CLE 211=";
	cin>>num4;
	 Average=(num1+num2+num3+num4)/4;
	 
	 if (Average>=90 && Average<=100)
	 { cout<<"Your average grade is"<<'\t'<<Average<<"/100"<<endl;
	 cout<<"Equivalent to HD"<<'\t'<<"its a HIGH DISTINCTION"<<endl;
	 }
	 
	 else if(Average>=80 && Average<=89)
	 {cout<<"Your average grade is"<<'\t'<<Average<<"/100"<<endl;
	 cout<<"Equivalent to D"<<'\t'<<"its a DISTINCTION"<<endl;
	 }
	 
	 else if(Average>=70 && Average<=79)
	 {cout<<"Your average grade is"<<'\t'<<Average<<"/100"<<endl;
	 cout<<"Equivalent to C"<<'\t'<<"its a CREDIT"<<endl;
	 }
	 
	 else if(Average>=60 && Average<=69)
	 { cout<<"Your average grade is"<<'\t'<<Average<<"/100"<<endl;
	 cout<<"Equivalent to UP"<<'\t'<<"its an UPPER PASS"<<endl;
	 }
	 
	 else if(Average>=50 && Average<=59)
	 { cout<<"Your average grade is"<<'\t'<<Average<<"/100"<<endl;
	 cout<<"Equivalent to P"<<'\t'<<"its a PASS"<<endl;
	 }
	 
	 else if(Average>=0 && Average<=49)
	 { cout<<"Your average grade is"<<'\t'<<Average<<"/100"<<endl;
	 cout<<"Equivalent to F"<<'\t'<<"its a FAIL"<<endl;
          }
           else
		 cout<<"Come to school";
	 
		 return 0;
}
It is possible, but hard... Because the names of the grades don't follow a pattern, you have to make an array to store them:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>//don't forget this!
...
string gradeNames[6] = {
	string("HIGH DISTINCTION"),
	string("DISTINCTION"),
	string("CREDIT"),
	string("UPPER PASS"),
	string("PASS"),
	string("FAIL"),
}
string gradeInitials[6] = {
	string("HD"),
	string("D"),
	string("C"),
	string("UP"),
	string("P"),
	string("F"),
}

And then use a for loop:
1
2
3
4
5
6
7
8
9
10
for(int i = 0; i < 6; i++) {
	//calculate the max grade and min grade. we do not worry about exceptions like 100 and 0 here
	int maxGrade = (10 - i) * 100 - 1;
	int minGrade = maxGrade - 9;
	//if statement is a bit complex due to the fact we have to consider maxGrade is 100 for HIGH DISTINCTION and minGrade is 0 for FAIL
	if(Average >= ((i != 5) ? minGrade : 0) && Average <= ((i != 0) ? maxGrade : 100)) {
		cout << "Your average grade is\t" << Average << "/100" << endl;
		cout << "Equivalent to " << gradeInitials[i] << '\t' << "its a " << gradeNames[i];
	}
}


But I think this will not be worth it... Anyway it's yours to decide if it is or not.
Similar to Tyler T's solution but using const char * for the table (smaller and faster) and a simplified loop.
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
#include <iostream>
using namespace std;

struct GradeInfo
{
    const char *name;
    const char *initials;
    int high;
};

GradeInfo grades[] = {
    {"FAIL", "F", 49},
    {"PASS", "P", 59},
    {"UPPER PASS", "UP", 69},
    {"CREDIT", "C", 79},
    {"DISTINCTION", "D", 89},
    {"HIGH DISTINCTION", "HD", 100},
};

int
main()
{
    int num1, num2, num3, num4, Average;

    cout << "Enter your grade in the following units." << endl;
    cout << "COT 211=";
    cin >> num1;

    cout << "COT 212=";
    cin >> num2;

    cout << "COT 213=";
    cin >> num3;

    cout << "CLE 211=";
    cin >> num4;
    Average = (num1 + num2 + num3 + num4) / 4;

    cout << "Your average grade is" << '\t' << Average << "/100" << endl;

    bool found = false;
  for (auto & grade:grades) {
        if (Average <= grade.high) {
            cout << "Equivalent to " << grade.initials << '\t'
                << "its a " << grade.name << '\n';
            found = true;
            break;
        }
    }

    if (!found) {
        cout << "Come to school\n";
    }
    return 0;
}

thanks guys ...
Topic archived. No new replies allowed.