fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. // Define a structure to hold student information
  8. struct Student {
  9. string name;
  10. int age;
  11. string major;
  12. };
  13.  
  14. // Function to input student information
  15. void inputStudentInfo(Student &student) {
  16. cout << "Enter student name: ";
  17. getline(cin, student.name);
  18. cout << "Enter student age: ";
  19. cin >> student.age;
  20. cin.ignore(); // Clear the newline character from the input buffer
  21. cout << "Enter student major: ";
  22. getline(cin, student.major);
  23. }
  24.  
  25. // Function to display student information
  26. void displayStudentInfo(const Student &student) {
  27. cout << "Name: " << student.name << endl;
  28. cout << "Age: " << student.age << endl;
  29. cout << "Major: " << student.major << endl;
  30. }
  31.  
  32. int main() {
  33. int numberOfStudents;
  34. cout << "Enter the number of students: ";
  35. cin >> numberOfStudents;
  36. cin.ignore(); // Clear the newline character from the input buffer
  37.  
  38. vector<Student> students(numberOfStudents);
  39.  
  40. // Input information for each student
  41. for (int i = 0; i < numberOfStudents; ++i) {
  42. cout << "Entering information for student " << (i + 1) << ":" << endl;
  43. inputStudentInfo(students[i]);
  44. }
  45.  
  46. // Display information for each student
  47. cout << "\nStudent Information:\n";
  48. for (const auto &student : students) {
  49. displayStudentInfo(student);
  50. cout << "------------------------" << endl;
  51. }
  52.  
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Enter the number of students: 
Student Information: