Answer to Question #225445 in C++ for nawab ul haq

Question #225445

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.


1
Expert's answer
2021-08-12T02:32:56-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS