Develop a C++ code for a function that takes the GPA of students from the user; this function should work for different class sizes specified by the user (class size may be 30, 40 or 50). Function should display the GPA of those students who has GPA more than 3.5.
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
void SetName(string n) { name = n; }
string GetName() { return name; }
void SetGPA(double x) { gpa = x; }
double GetGPA() { return gpa; }
private:
string name;
double gpa;
};
int main() {
int n;
Student *students;
cout << "Enter a size of the class: ";
cin >> n;
students = new Student[n];
for (int i=0; i<n; i++) {
cout << "Enter a name of the " << i+1 << " student: ";
string name;
cin.ignore();
getline(cin, name);
students[i].SetName(name);
cout << "Enter his/her GPA: ";
double gpa;
cin >> gpa;
students[i].SetGPA(gpa);
}
cout << endl << "The list of students who has passed:" << endl;
for (int i=0; i<n; i++) {
if (students[i].GetGPA() > 3.5) {
cout << students[i].GetName() << " " << students[i].GetGPA() << endl;
}
}
delete [] students;
}
Comments
Leave a comment