#include <iostream>
#include <string>
#include <vector>
// Define the struct
struct Student {
int student_id;
std::string name_id;
};
int main() {
// Using a vector to easily handle up to 10 students
std::vector<Student> students;
const int MAX_STUDENTS = 10;
int count = 0;
std::cout << "--- Enter Student Details (Max " << MAX_STUDENTS << ") ---\n";
std::cout << "Type a negative ID or leave the name empty to stop early.\n\n";
for (int i = 0; i < MAX_STUDENTS; ++i) {
Student tempStudent;
std::cout << "Student #" << (i + 1) << " ID: ";
std::cin >> tempStudent.student_id;
// Check if user wants to exit based on ID
if (tempStudent.student_id < 0) {
break;
}
// Clear the input buffer before reading a string with spaces
std::cin.ignore();
std::cout << "Student #" << (i + 1) << " Name: ";
std::getline(std::cin, tempStudent.name_id);
// Check if user wants to exit based on empty name
if (tempStudent.name_id.empty()) {
break;
}
// Store the valid student into our vector
students.push_back(tempStudent);
count++;
}
// Display the results
std::cout << "\n-----------------------------------\n";
std::cout << " STUDENT LIST \n";
std::cout << "-----------------------------------\n";
if (students.empty()) {
std::cout << "No student records entered.\n";
} else {
for (int i = 0; i < count; ++i) {
std::cout << "ID: " << students[i].student_id
<< " | Name: " << students[i].name_id << "\n";
}
}
std::cout << "-----------------------------------\n";
return 0;
}

Tidak ada komentar:
Posting Komentar