hi, i need help with my coding because the calculation of the cgpa doesnt appear :(

my calculation of cgpa doesnt appear. please 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
  #include <iostream>
#include <sstream>

using namespace std; 
struct gpa
{
 string grade[4];
 int credit[4], 
 point[4];
}
student; 
double pointcal(string grade);
void calculate_gpa();
void print(); 

int main()

{  
cout<<"\nENTER GRADE FOR THE FOLLOWING SUBJECT:-"<<endl; 
cout<<"LOGIC AND CRITICAL THINKING (SFES1101) :";
cin>> student.grade[0];
cout<<"SUBJECT CREDIT :";
cin >> student.credit[0];    
cout<<"INOVATION AND ORGANIZATION (SFES1322) : " ;
cin>> student.grade[1];
cout<<"SUBJECT CREDIT :";
cin >> student.credit[1]; 
cout<<"BASIC MINERALOGI (SGES1274) :";
cin>> student.grade[2];
cout<<"SUBJECT CREDIT :";
cin >> student.credit[2]; 
cout<<"BASIC MINERALS, ROCKS AND FOSSIL (SGES1275) :";
cin>> student.grade[3];
cout<<"SUBJECT CREDIT :";
cin >> student.credit[3]; 
cout<<"BIOCOMPUTING (SHES1603) :  ";
cin>> student.grade[4];
cout<<"SUBJECT CREDIT :";
cin >> student.credit[4]; 
print(); 
} 
double pointcal(string grade)
{ 
    
 {          
 if (grade=="A" || grade=="a") { return 4.0;  }
 else if (grade=="A-" || grade=="a-") { return 3.75; }
 else if (grade=="B+" || grade=="b+") { return 3.5; }
 else if (grade=="B" || grade=="b") { return 3.25; }
 else if (grade=="B-" || grade=="b-") { return 3.0; }
 else if (grade=="C+" || grade=="c+") { return 2.75; } 
 else if (grade=="C" || grade=="c") { return 2.5; }
 else if (grade=="C-" || grade=="c-") { return 2.25; }
 else if (grade=="D" || grade=="d") { return 2.0; }
 else if (grade=="E" || grade=="e") { return 1.5; }
 else if (grade=="F" || grade=="f") { return 1.25; }  
 else { return 0; }
}

} 
void calculate_gpa()
{ 
double gpa_s;
double total_point = 0;
double total_credit = 0;
int i; for (i=0; i<7; i++) {
 student.point[i] = pointcal(student.grade[i]) * student.credit[i];
 total_point += student.point[i];
 total_credit += student.credit[i];
}
 
gpa_s = total_point / total_credit;
cout<<"GPA: "<<gpa_s<<endl; }
void print()
{
cout<<endl;
cout<<"\nLOGIC AND CRITICAL THINKING (SFES1101) :"<<student.grade[0];
cout<<"\nINOVATION AND ORGANIZATION (SFES1322) : "<<student.grade[1];
cout<<"\nBASIC MINERALOGI (SGES1274) :"<<student.grade[2];
cout<<"\nBASIC MINERALS, ROCKS AND FOSSIL (SGES1275) :"<<student.grade[3];
cout<<"\nBIOCOMPUTING (SHES1603) :"<<student.grade[4]<<endl;
calculate_gpa();
}
Pick an indentation style. https://en.wikipedia.org/wiki/Indentation_style
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
#include <iostream>
#include <sstream>
using namespace std;

struct gpa {
  string grade[4];
  int credit[4], point[4];
} student;

double pointcal(string grade);
void calculate_gpa();
void print();

int main()
{
  cout << "\nENTER GRADE FOR THE FOLLOWING SUBJECT:-" << endl;
  cout << "LOGIC AND CRITICAL THINKING (SFES1101) :";
  cin >> student.grade[0];
  cout << "SUBJECT CREDIT :";
  cin >> student.credit[0];
  cout << "INOVATION AND ORGANIZATION (SFES1322) : ";
  cin >> student.grade[1];
  cout << "SUBJECT CREDIT :";
  cin >> student.credit[1];
  cout << "BASIC MINERALOGI (SGES1274) :";
  cin >> student.grade[2];
  cout << "SUBJECT CREDIT :";
  cin >> student.credit[2];
  cout << "BASIC MINERALS, ROCKS AND FOSSIL (SGES1275) :";
  cin >> student.grade[3];
  cout << "SUBJECT CREDIT :";
  cin >> student.credit[3];
  cout << "BIOCOMPUTING (SHES1603) :  ";
  cin >> student.grade[4];
  cout << "SUBJECT CREDIT :";
  cin >> student.credit[4];
  print();
}

double pointcal(string grade)
{
  {
    if (grade == "A" || grade == "a") {
      return 4.0;
    } else if (grade == "A-" || grade == "a-") {
      return 3.75;
    } else if (grade == "B+" || grade == "b+") {
      return 3.5;
    } else if (grade == "B" || grade == "b") {
      return 3.25;
    } else if (grade == "B-" || grade == "b-") {
      return 3.0;
    } else if (grade == "C+" || grade == "c+") {
      return 2.75;
    } else if (grade == "C" || grade == "c") {
      return 2.5;
    } else if (grade == "C-" || grade == "c-") {
      return 2.25;
    } else if (grade == "D" || grade == "d") {
      return 2.0;
    } else if (grade == "E" || grade == "e") {
      return 1.5;
    } else if (grade == "F" || grade == "f") {
      return 1.25;
    } else {
      return 0;
    }
  }
}

void calculate_gpa()
{
  double gpa_s;
  double total_point = 0;
  double total_credit = 0;
  int i;
  for (i = 0; i < 7; i++) {
    student.point[i] = pointcal(student.grade[i]) * student.credit[i];
    total_point += student.point[i];
    total_credit += student.credit[i];
  }
  gpa_s = total_point / total_credit;
  cout << "GPA: " << gpa_s << endl;
}

void print()
{
  cout << endl;
  cout << "\nLOGIC AND CRITICAL THINKING (SFES1101) :" << student.grade[0];
  cout << "\nINOVATION AND ORGANIZATION (SFES1322) : " << student.grade[1];
  cout << "\nBASIC MINERALOGI (SGES1274) :" << student.grade[2];
  cout << "\nBASIC MINERALS, ROCKS AND FOSSIL (SGES1275) :" << student.grade[3];
  cout << "\nBIOCOMPUTING (SHES1603) :" << student.grade[4] << endl;
  calculate_gpa();
}


> cin >> student.credit[4];
...
> for (i = 0; i < 7; i++)
...
> student.grade[4]
Your array is only 4 elements, so your valid subscripts are 0 to 3 only.
Accessing outside your arrays will just get you trouble.


For a slightly different take on this using an array for the subject names, consider (without input validation):

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
#include <iostream>
#include <iterator>
#include <iomanip>

constexpr const char* const subnames[] {"LOGIC AND CRITICAL THINKING (SFES1101)", "INOVATION AND ORGANIZATION (SFES1322)",
		"BASIC MINERALOGI (SGES1274)", "BASIC MINERALS, ROCKS AND FOSSIL(SGES1275)",
		"BIOCOMPUTING (SHES1603)"};

constexpr size_t NoSubs {std::size(subnames)};

struct Subject {
	std::string grade;
	int credit {};
	int	point {};
};

double pointcal(const std::string& grade);
void calculate_gpa(Subject subjects[NoSubs]);
void print(Subject subjects[NoSubs]);

int main()
{
	Subject subjects[NoSubs] {};

	std::cout << "\nEnter grade and credit for the following subjects:-\n";

	for (size_t s = 0; s < NoSubs; ++s) {
		std::cout << subnames[s] << "\nGrade: ";
		std::cin >> subjects[s].grade;

		std::cout << "Credit: ";
		std::cin >> subjects[s].credit;
	}

	print(subjects);
}

double pointcal(const std::string& grade)
{
	if (grade == "A" || grade == "a") return 4.0;
	else if (grade == "A-" || grade == "a-") return 3.75;
	else if (grade == "B+" || grade == "b+") return 3.5;
	else if (grade == "B" || grade == "b") return 3.25;
	else if (grade == "B-" || grade == "b-") return 3.0;
	else if (grade == "C+" || grade == "c+") return 2.75;
	else if (grade == "C" || grade == "c") return 2.5;
	else if (grade == "C-" || grade == "c-") return 2.25;
	else if (grade == "D" || grade == "d") return 2.0;
	else if (grade == "E" || grade == "e") return 1.5;
	else if (grade == "F" || grade == "f") return 1.25;
	else return 0;
}

void calculate_gpa(Subject subjects[NoSubs])
{
	double total_point {};
	double total_credit {};

	for (size_t i = 0; i < NoSubs; ++i) {
		subjects[i].point = pointcal(subjects[i].grade) * subjects[i].credit;
		total_point += subjects[i].point;
		total_credit += subjects[i].credit;
	}

	std::cout << "\nGPA: " << total_point / total_credit << '\n';
}

void print(Subject subjects[NoSubs])
{
	std::cout << '\n';

	for (size_t i = 0; i < NoSubs; ++i)
		std::cout << std::left << std::setw(46) << subnames[i] << ": " << subjects[i].grade << '\n';
		calculate_gpa(subjects);
}

Topic archived. No new replies allowed.