Write a C++ program to read and print details of N students. (Note Modify Question number 2)
a. Hints
i. Create a class to hold all student attributes, a method which interact with a user and read student details, method to print/write student details.
ii. Create a main function which will prompt a user to inter N student (Number of students to read and write their details), call function from a class above to read and write student detail.
iii. The user interactive screen and output should appear Sample output
Enter total number of students: 2
Enter details of student 1:
Enter RegNo:101
Enter Name: John Joseph
Enter Course: DIT
Enter Subject: C++ Programming
Enter Score:85.5 Enter details of student 2:
Enter Registration number:102
Enter Name: Josephine Baraka
Enter Course: DIT
Enter Subject: C++ Programming
Enter Score:45
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class details {
int regno = 0;
string name, course, subject;
float score = 0;
public:
void setRegNo() { cin >> this->regno; }
void setName() { cin >> this->name; }
void setCourse() { cin >> this->course; }
void setSubject() { cin >> this->subject; }
void setScore() { cin >> this->score; }
int getRegNo() { return this->regno; }
string getName() { return this->name; }
string getCourse() { return this->course; }
string getSubject() { return this->subject; }
float getScore() { return this->score; }
};
int main() {
int numOfStudents;
cout << "Enter total number of students: ";
cin >> numOfStudents;
vector<details> all;
for (int i = 1; i <= numOfStudents; i++) {
details student;
cout << "Enter details of student " << i << ": \n";
cout << "Enter Registration number: ";
student.setRegNo();
cout << "Enter Name: ";
student.setName();
cout << "Enter Course: ";
student.setCourse();
cout << "Enter Subject: ";
student.setSubject();
cout << "Enter Score: ";
student.setScore();
all.push_back(student);
}
cout << endl;
for (size_t i = 0; i < all.size(); i++) {
cout << "Details of student " << i+1 << ": \n";
cout << "Registration number: " << all[i].getRegNo() << "\n";
cout << "Name: " << all[i].getName() << "\n";
cout << "Course: " << all[i].getCourse() << "\n";
cout << "Subject: " << all[i].getSubject() << "\n";
cout << "Score: " << all[i].getScore() << "\n";
}
return 0;
}
Comments
Leave a comment