fork download
  1. #include<string>
  2. #include<vector>
  3. #include<iostream>
  4.  
  5. typedef struct
  6. {
  7. std::string name;
  8. int age;
  9. }student;
  10.  
  11. // Input info
  12. void input_info(std::vector<student> &s)
  13. {
  14. student obj;
  15. std::cout<<"\nEnter Students name";
  16. std::cin>>obj.name;
  17. std::cout<<"\nEnter Students age";
  18. std::cin>>obj.age;
  19. s.push_back(obj);
  20. }
  21.  
  22. // Output info
  23. void output_info(const std::vector<student> &s)
  24. {
  25. for (auto itr = s.cbegin(); itr != s.cend(); ++itr)
  26. {
  27. std::cout<<"\nName:"<< itr->name;
  28. std::cout<<"\nAge:"<< itr->age;
  29. }
  30. }
  31.  
  32. int main()
  33. {
  34. int n;
  35. std::cout<<"How many students you want to input?\n";
  36. std::cin>>n;
  37. std::vector<student>s;
  38. for(int i = 0; i<n; i++)
  39. {
  40. input_info(s);
  41. }
  42. output_info(s);
  43. return 0;
  44. }
Success #stdin #stdout 0s 3036KB
stdin
2
Student1
18
Student2
25
Student3
30
stdout
How many students you want to input?

Enter Students name
Enter Students age
Enter Students name
Enter Students age
Name:Student1
Age:18
Name:Student2
Age:25