Can't Write this Program

Hey guys i need your help. I have a class assignment to write a program that compute students cgpa and i don't have the least idea on how to go about it. I am only a beginner with C++
I'm sure your professor/teacher has given you instructions on how to do certain functions. In order to calculate GPA, you probably need to do the following:

1. Input a set of grades. This could be reading from a file or asking the user for input.

2. Convert each grade to a numeric value

3. Get the average value (note - make sure you don't end up with an integer result)

4. Handle the result. This could be writing to an output file or displaying the value on the screen.

What you should do is write the program in steps. Write code for the step and test it to make sure that it works.

Show us your code when you have something started and we can help. We are not going to do your homework for you.
Try using a while loop to add up all of the values and average them out. I'll post a link to a video where this is done just with test scores instead of gpa.

https://www.youtube.com/watch?v=gfJ09HoStHg

It might actually be the video after this one in the playlist. See for yourself :)
Last edited on
Wow......
Thanks guys
Show you what i did in a jiffy
Lemme get that from my compiler
Guys, look what I've done. Don't know how to proceed from her to calculate GPA and CGPA
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
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
	//this program will compute students cgpa
	string student_name[10],matric_no[10],sub_code[10],sub_name[10];
	float gpa,cgpa,total_points1[10],total_points2[10],total_credit_hrs[10],total_points[10];
	int   score,credit_hr_sem1,credit_hr_sem2,sem,total_sem_taken,total_sub_taken,sub_credit_hr,total_credit_hr;
	char sub_grade;
	cout<<"enter student_name"<<endl;
	cin>>student_name[10];
	cout<<"matric_no"<<endl;
	cin>>matric_no[10];
	cout<<"total_sem_taken"<<endl;
	cin>>total_sem_taken;
	cout<<"sub_code"<<endl;
	cin>>sub_code[10];
	cout<<"sub_name"<<endl;
	cin>>sub_name[10];
	cout<<"sub_grade"<<endl;
	cin>>sub_grade;
	cout<<"sub_credit_hr"<<endl;
	cin>>sub_credit_hr;
	cout<<"enter sub_grade"<<endl;
	cin>>sub_grade;
	if(score>=90)cout<<"sub_grade==A+"<<endl;
	else if (score>=80)cout<<"sub_grade==A"<<endl;
	else if (score>=70)cout<<"sub_grade==A-"<<endl;
	else if (score>=60)cout<<"sub_grade==B+"<<endl;
	else if (score>=50)cout<<"sub_grade==B"<<endl;
	else if (score>=40)cout<<"sub_grade==C"<<endl;
	else if (score>=30)cout<<"sub_grade==D"<<endl;
	else cout<<"sub_grade==F"<<endl;
	{
		float grade_sem1,grade_sem2,total_points1[10];
		int b, grade [b];
		cout<<"enter total_points1[10]"<<endl;
		cin>>total_points1[10];
		for(int b=0; b<grade_sem1; b++)
		
		
	}
	
}
Last edited on
Besides, the real issue I have from here is calculation of the GPA and CGPA. I don't know how to use loops. Pls i need your help
See what i found but it is not calculating the GPA and CGPA for me as it should.
Can someone please look at this for me? It would greatly help
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
#include<iostream>		// for input and output
#include<cstring>		// for string nama...
#include<cmath>			//for mathematical operations
using namespace std;		// for name space..

struct student	// declare struct
{
	/*this program will calculate students CGPA*/
	string name,no_matric;
	string sub_code[10],sub_name[10],sub_grade[10];
	float credit_hour[10];
}student1;

int main()
{
	int credit_hours[10];
	int sem,total_subject;		
	float value[10],gpa[10],get_point[10],cgpa[10];
	float total_point_sem = 0; 		//Initialise value
	int total_credit_hours_sem = 0; 	//Initialise value
	cin.ignore();
	
		cout << "\n ENTER STUDENT'S NAME	: ";		//Enter student's name
	getline(cin,student1.name);
	cout << " ENTER STUDENT'S MATRIC NUMBER  : "; 		//Enter student's matric number
	getline(cin,student1.no_matric);
	cout << " ENTER TOTAL SEM TAKEN	:"; 		//Total semester taken by the student
	cin >> sem;
	
	for(int a=0; a<sem ; a++) 	//'a' declared as int in for()[newly added]
	{ 
		int b;  //newly added line,'b' choose to add it here because, if not, error will be occured : 'b' for gpa[b] will become undefined...

		int total_credit_hours = 0;		//newly added
		float total_point = 0, gpa[b]; 		//newly added
		float cgpa;
		cgpa==gpa[b];
		cout << "\n ENTER SUBJECT TAKEN FOR SEM	 : " << a+1 << ":"; //Enter total number of subject taken for each sem
		cin >> total_subject;
		for(int b=0; b<total_subject; b++) //loop for the courses
		{
			cin.ignore();
			cout << " ENTER SUBJECT CODE	: ";  //Enter subject code
			getline(cin,student1.sub_code[b]);
			cout << " ENTER SUBJECT NAME	: ";  //Enter subject name
			getline(cin,student1.sub_name[b]);
			cout << " ENTER SUBJECT'S CREDIT HOUR	: ";  //Enter credit hours of the subject
			cin >> student1.credit_hour[b];
			cin.ignore();
			cout << " ENTER SUBJECT GRADE	: ";  //Enter grade gained by student for every subject
			getline(cin,student1.sub_grade[b]);
			
			if(student1.sub_grade[b]=="A")
				value[b]=4;
			else if(student1.sub_grade[b]=="A-")
				value[b]=3.75;
			else if(student1.sub_grade[b]=="B+")
				value[b]=3.5;
			else if(student1.sub_grade[b]=="B")
				value[b]=3;
			else if(student1.sub_grade[b]=="B-")
				value[b]=2.75;
			else if(student1.sub_grade[b]=="C+")
				value[b]=2.5;
			else if(student1.sub_grade[b]=="C")
				value[b]=2;
			else if(student1.sub_grade[b]=="C-")
				value[b]=1.75;
			else if(student1.sub_grade[b]== "D+")
				value[b]=1.5;
			else if(student1.sub_grade[b]=="D")
				value[b]=1;
			else 
				value[b]=0;
			
			get_point[b] = student1.credit_hour[b] * value[b]; //multiply of credit hours and grade get for each subject
			total_credit_hours +=student1.credit_hour[b]; //total cerdit hours for a sem
			total_point += get_point[b]; //total points(multiple of credits hours and grade)get for a sem 
		}
		
		gpa[b] = total_point / total_credit_hours; //calculate GPA for each sem
		
		total_point_sem += total_point; //Total points(multiple of grade and credit hours) get for all semester
		
		total_credit_hours_sem += total_credit_hours; //credits hour get for all semester
	}
	
	cgpa [10]= total_point_sem / total_credit_hours_sem;  //calculate CGPA
	
	cout << "\n =================================================== ";
	cout <<"\n NAME			: " << student1.name << endl;  //display name of student
	cout <<" NO MATRIC	: "<< student1.no_matric << endl;
	for(int a=0; a<sem; a++) //Declaration of 'a' as int in for()
	{ 
		cout <<" SUBJECT CODE \t" << " SUBJECT NAME \t" << " SUBJECT CREDIT HOURS " <<endl;
		for(int b=0; b<total_subject; b++) //Declaration of 'b' as int in for()
		{
			cout << " " << student1.sub_code[b] << " : " << student1.sub_name[b]<< " : " << student1.credit_hour[b] << endl;
		}
			cout << " GPA" <<a+1<< " : " <<gpa[a] << endl; 
	} 
			cout << " CGPA : "<<cgpa<<endl;
			return 0;
		}
Last edited on
Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/
Thank you MikeyBoy
Here:
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
#include<iostream>	// for input and output
#include<cstring>	// for string nama...
#include<cmath>	//for mathematical operations
using namespace std;	// for name space..

struct student	// declare struct
{
/*this program will calculate students CGPA*/
string name,no_matric;
string sub_code[10],sub_name[10],sub_grade[10];
float credit_hour[10];
}student1;

int main()
{
int credit_hours[10];
int sem,total_subject;	
float value[10],gpa[10],get_point[10],cgpa[10];
float total_point_sem = 0; //Initialise value
int total_credit_hours_sem = 0; //Initialise value
cin.ignore();

cout << "\n ENTER STUDENT'S NAME	: ";	//Enter student's name
getline(cin,student1.name);
cout << " ENTER STUDENT'S MATRIC NUMBER : "; //Enter student's matric number
getline(cin,student1.no_matric);
cout << " ENTER TOTAL SEM TAKEN	:"; //Total semester taken by the student
cin >> sem;

for(int a=0; a<sem ; a++) //'a' declared as int in for()[newly added]
{ 
int b; //newly added line,'b' choose to add it here because, if not, error will be occured : 'b' for gpa[b] will become undefined...

int total_credit_hours = 0;	//newly added
float total_point = 0, gpa[b]; //newly added
float cgpa;
cgpa==gpa[b];
cout << "\n ENTER SUBJECT TAKEN FOR SEM	: " << a+1 << ":"; //Enter total number of subject taken for each sem
cin >> total_subject;
for(int b=0; b<total_subject; b++) //loop for the courses
{
cin.ignore();
cout << " ENTER SUBJECT CODE	: "; //Enter subject code
getline(cin,student1.sub_code[b]);
cout << " ENTER SUBJECT NAME	: "; //Enter subject name
getline(cin,student1.sub_name[b]);
cout << " ENTER SUBJECT'S CREDIT HOUR	: "; //Enter credit hours of the subject
cin >> student1.credit_hour[b];
cin.ignore();
cout << " ENTER SUBJECT GRADE	: "; //Enter grade gained by student for every subject
getline(cin,student1.sub_grade[b]);

if(student1.sub_grade[b]=="A")
value[b]=4;
else if(student1.sub_grade[b]=="A-")
value[b]=3.75;
else if(student1.sub_grade[b]=="B+")
value[b]=3.5;
else if(student1.sub_grade[b]=="B")
value[b]=3;
else if(student1.sub_grade[b]=="B-")
value[b]=2.75;
else if(student1.sub_grade[b]=="C+")
value[b]=2.5;
else if(student1.sub_grade[b]=="C")
value[b]=2;
else if(student1.sub_grade[b]=="C-")
value[b]=1.75;
else if(student1.sub_grade[b]== "D+")
value[b]=1.5;
else if(student1.sub_grade[b]=="D")
value[b]=1;
else 
value[b]=0;

get_point[b] = student1.credit_hour[b] * value[b]; //multiply of credit hours and grade get for each subject
total_credit_hours +=student1.credit_hour[b]; //total cerdit hours for a sem
total_point += get_point[b]; //total points(multiple of credits hours and grade)get for a sem 
}

gpa[b] = total_point / total_credit_hours; //calculate GPA for each sem

total_point_sem += total_point; //Total points(multiple of grade and credit hours) get for all semester

total_credit_hours_sem += total_credit_hours; //credits hour get for all semester
}

cgpa [10]= total_point_sem / total_credit_hours_sem; //calculate CGPA

cout << "\n =================================================== ";
cout <<"\n NAME	: " << student1.name << endl; //display name of student
cout <<" NO MATRIC	: "<< student1.no_matric << endl;
for(int a=0; a<sem; a++) //Declaration of 'a' as int in for()
{ 
cout <<" SUBJECT CODE \t" << " SUBJECT NAME \t" << " SUBJECT CREDIT HOURS " <<endl;
for(int b=0; b<total_subject; b++) //Declaration of 'b' as int in for()
{
cout << " " << student1.sub_code[b] << " : " << student1.sub_name[b]<< " : " << student1.credit_hour[b] << endl;
}
cout << " GPA" <<a+1<< " : " <<gpa[a] << endl; 
} 
cout << " CGPA : "<<cgpa<<endl;
return 0;
}

Last edited on
I have been trying to work on this for the past 5-hrs today but both the GPA and CGPA and returning negative values
I am submitting this assignment tomorrow....but trust me it is not easy to write programs as a beginner
At line 32, you declare a variable b, but you don't initialize it.

At line 35, you attempt to use it as the dimension of the array gpa. This is wrong for 2 reasons:

- it's not legal C++. You can only use constants to specify the size of an array.
- even if it were constant, you haven't given it a value. What size do you think your gpa array will be?

Then, at line 40, you declare a new variable, also called b. This is a different variable from the one at line 32, and is only in scope inside the loop. For lines 40 - 79, you are using this new b, which has a value that is incremented with each time around the look.

However, at line 81, you're outside the loop, and back to using the b that you declared at line 35. You still haven't given it a value, so which element of gpa do you think you're accessing here?

There are 2 things to learn here:

1) Don't use variables that you haven't initialised. Always initialise your variables with a value.

2) Don't hide a variable with another one of the same name. This will confuse you, and lead to errors.

EDIT: Thanks for going back and editing your posts to use code tags. That really helps us read your code more easily :)

Last edited on
Wow...................
Thanks a bunch!

I will implement your inputs and see how it runs
brb...............
the program is still returning errors after implementing suggestions......pls any further assistance?
I have got an extension to submit tomorrow but the GPA and CGPA are returning 0 values and letters in between
Without seeing the current state of your code, how can we know where the problems might be?
I have been trying in vain to sen my code....it is not sending
Topic archived. No new replies allowed.