Use C++
Write a program that manages students’records. Each student’s record consists of a netID, a major and a GPA, maintained in parallel arrays. The program will read students’ data from a file and loop on displaying the following menu of choices:
1.List the top n students
2. Search for a student
3. Exit the program.
If the user chooses 1, the user will be prompted for the value of n, and the netID, major and GPA of the top n students will be listed, starting with the student with highest GPA and going down.
If the user chooses 2, user will be prompted to enter the student’s netID. If the netID is found in the netID array, the array index, major and GPA of the student are displayed. If the student is not found, a “Student not found” message is displayed.
If the user chooses 3, the program prints “Exiting\n” and terminates.
Guide On Rating System
Vote
Sure! Here's an example program that manages students' records using C++:
```cpp
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <algorithm>
#include <unordered_map>
struct Student {
std::string netID;
std::string major;
double GPA;
};
// Function to read student data from a file
std::vector<Student> readDataFromFile(const std::string& filename) {
std::vector<Student> students;
std::ifstream inputFile(filename);
if (inputFile.is_open()) {
std::string netID, major;
double GPA;
while (inputFile >> netID >> major >> GPA) {
students.push_back({ netID, major, GPA });
}
inputFile.close();
}
return students;
}
// Function to display a student's data
void displayStudent(const Student& student) {
std::cout << "NetID: " << student.netID << std::endl;
std::cout << "Major: " << student.major << std::endl;
std::cout << "GPA: " << std::fixed << std::setprecision(2) << student.GPA << std::endl;
}
// Function to list the top n students
void listTopStudents(const std::vector<Student>& students, int n) {
std::vector<Student> sortedStudents = students;
std::sort(sortedStudents.begin(), sortedStudents.end(), [](const Student& s1, const Student& s2) {
return s1.GPA > s2.GPA;
});
for (int i = 0; i < std::min(n, static_cast<int>(sortedStudents.size())); ++i) {
std::cout << "Student " << i + 1 << ":" << std::endl;
displayStudent(sortedStudents[i]);
std::cout << std::endl;
}
}
// Function to search for a student by netID
void searchForStudent(const std::unordered_map<std::string, int>& netIdToIndex, const std::vector<Student>& students, const std::string& netID) {
auto it = netIdToIndex.find(netID);
if (it != netIdToIndex.end()) {
std::cout << "Student found!" << std::endl;
displayStudent(students[it->second]);
} else {
std::cout << "Student not found" << std::endl;
}
}
int main() {
std::vector<Student> students = readDataFromFile("students.txt");
// Creating a map of netID to index for efficient searching
std::unordered_map<std::string, int> netIdToIndex;
for (size_t i = 0; i < students.size(); ++i) {
netIdToIndex[students[i].netID] = i;
}
int choice;
do {
std::cout << "Menu:" << std::endl;
std::cout << "1. List the top n students" << std::endl;
std::cout << "2. Search for a student" << std::endl;
std::cout << "3. Exit the program" << std::endl;
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1: {
int n;
std::cout << "Enter the value of n: ";
std::cin >> n;
std::cout << std::endl;
listTopStudents(students, n);
break;
}
case 2: {
std::string netID;
std::cout << "Enter the student's netID: ";
std::cin >> netID;
std::cout << std::endl;
searchForStudent(netIdToIndex, students, netID);
break;
}
case 3:
std::cout << "Exiting" << std::endl;
break;
default:
std::cout << "Invalid choice. Please try again." << std::endl;
break;
}
std::cout << std::endl;
} while (choice != 3);
return 0;
}
```
Assuming you have stored the students' data in a text file called "students.txt" with each line containing a student's netID, major, and GPA separated by spaces, this program will read the data from the file and allow the user to interact with the menu options.