Answer to Question #254575 in C++ for Kirrito

Question #254575
Create structure of Student with following data members Name Reg No CGPA Email ID Create array of 30 students in main function and provide following functionalities Initializing Students Inserting new Student Updating particular student record Deletion of particular student Searching of particular student Sorting all student
1
Expert's answer
2021-10-21T13:52:51-0400
#include <iostream>
#include <string>
using namespace std;


struct Student {
    string name;
    int regNo;
    double CGPA;
    string email;
};


void GetStudent(Student& st) {
    cout << "Enter a name of a student: ";
    getline(cin, st.name);
    cout << "Enter reg No: ";
    cin >> st.regNo;
    cout << "Enter CGPA: ";
    cin >> st.CGPA;
    cout << "Enter e-mail: ";
    cin >> st.email;
}


void PrintStudent(const Student& st) {
    cout << "Name: " << st.name << endl;
    cout << "Reg No: " << st.regNo << endl;
    cout << "CGPA: " << st.CGPA << endl;
    cout << "E-mail: " << st.email << endl;
}


void PrintStudents(Student a[], int size) {
    for (int i=0; i<size; i++) {
        PrintStudent(a[i]);
        cout << endl;
    }
}


int main() {
    Student students[30] = {{"Doe John", 12345, 4.5, "john@yahoo.com"},
                            {"Smith Adams", 23456, 5.0, "smith@gmail.com"},
                            {"Brown Bob", 13568, 3.3, "brown@gmail.com"},
                            {"Gates Bill", 98765, 4.2, "gates@microsoft.com"}};
    int nStudent = 4;


    cout << "Add a new student data: " << endl;
    GetStudent(students[nStudent]);
    nStudent++;


    cout << "Students list is:" << endl;
    PrintStudents(students, nStudent);
    cout << endl;


    string name;
    cin.ignore();
    cout << "Enter a name of the student you want to update: ";
    getline(cin, name);
    int i;
    for (i=0; i<nStudent; i++) {
        if (students[i].name == name) {
            break;
        }
    }
    if (i == nStudent) {
        cout << "The student does not found" << endl;
    }
    else {
        PrintStudent(students[i]);
        GetStudent(students[i]);
    }
    PrintStudents(students, nStudent);
    cout << endl;


    cin.ignore();
    cout << "Enter a name of the student you want to delete: ";
    getline(cin, name);
    for (i=0; i<nStudent; i++) {
        if (students[i].name == name) {
            break;
        }
    }
    if (i == nStudent) {
        cout << "The student does not found" << endl;
    }
    else {
        nStudent--;
        for (int j=i; j<nStudent; j++) {
            students[j] = students[j+1];
        }


        cout << "The student " << name << " was delated" << endl;
    }
    PrintStudents(students, nStudent);


    cout << endl << "Sorting students by name" << endl;
    int i_min;
    for (i=0; i<nStudent-1; i++) {
        i_min = i;
        for (int j=i+1; j<nStudent; j++) {
            if (students[j].name < students[i_min].name) {
                i_min = j;
            }
        }
        if (i_min != i) {
            Student tmp = students[i];
            students[i] = students[i_min];
            students[i_min] = tmp;
        }
    }
    PrintStudents(students, nStudent);
}

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