fork download
  1. #include <string>
  2. #include <vector>
  3. #include <iostream>
  4.  
  5. using std::cout;
  6. using std::cin;
  7. using std::vector;
  8. using std::string;
  9.  
  10. struct Person {
  11. string m_name;
  12. int m_initialMoney;
  13. int m_currentMoney;
  14.  
  15. Person(const std::string& name_, int money_=0)
  16. : m_name(name_)
  17. , m_initialMoney(0), m_currentMoney(0)
  18. {}
  19. };
  20.  
  21. int main ()
  22. {
  23. vector<Person> people;
  24.  
  25. // Obtain max number of names!
  26. size_t maxIndex = 0;
  27. cin >> maxIndex;
  28. if (maxIndex <= 0) {
  29. cout << "Nothing to do.\n";
  30. return 1;
  31. }
  32.  
  33. // Create a person for every name...
  34. for (size_t index = 0; maxIndex != index; index++) {
  35. std::string newName;
  36. cin >> newName;
  37. cout << "Inserting " << people.size() << ": " << newName << std::endl;
  38. people.emplace_back(newName);
  39. }
  40.  
  41. // Check to see if the person's name is being read correctly
  42. cout << "Checking if the proper person's name is there: \n";
  43. for (auto& person : people) {
  44. cout << "Reading " << person.m_name << std::endl;
  45. }
  46.  
  47. cout << "Changing a single element of an array: \n";
  48. people[1].m_name = "John";
  49.  
  50. for (auto& person : people) {
  51. cout << "Reading " << person.m_name << std::endl;
  52. }
  53.  
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0s 3280KB
stdin
5
dave
laura
owen
vick
amr
stdout
Inserting 0: dave
Inserting 1: laura
Inserting 2: owen
Inserting 3: vick
Inserting 4: amr
Checking if the proper person's name is there: 
Reading dave
Reading laura
Reading owen
Reading vick
Reading amr
Changing a single element of an array: 
Reading dave
Reading John
Reading owen
Reading vick
Reading amr