X39

Untitled

X39
Jun 23rd, 2015
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. struct Person {
  2.     std::string name;
  3.     int initialMoney;
  4.     int currentMoney;
  5.     Person(const std::string &name, int money = 0, int currentMoney = 0)
  6.     {
  7.         this->name = name;
  8.         this->initialMoney = money;
  9.         this->currentMoney = currentMoney;
  10.     }
  11. };
  12.  
  13. int main()
  14. {
  15.     std::vector<Person> persons;
  16.     int index = 0;
  17.     int maxIndex = 0;
  18.  
  19.     std::ofstream fout("gift1.out");
  20.     std::ifstream fin("gift1.in");
  21.  
  22.     // Obtain max number of names!
  23.     fin >> maxIndex;
  24.  
  25.     // Create a person for every name...
  26.     for (index = 0; maxIndex != index; index++) {
  27.         std::string tmpName;
  28.         fin >> tmpName;
  29.         std::cout << "Inserting " << index << ": " << tmpName << std::endl;
  30.         persons.push_back(Person(tmpName, 0, 0));
  31.     }
  32.  
  33.     std::cout << "Checking if the proper person's name is there: \n";
  34.     // Check to see if the person's name is being read correctly
  35.     for (index = 0; maxIndex != index; index++) {
  36.         std::cout << "Reading " << index << ":" << persons[index].name << std::endl;
  37.     }
  38.  
  39.     std::cout << "Changing a single element of an array: \n";
  40.     persons[1].name = (char *)"John";
  41.  
  42.     for (index = 0; maxIndex != index; index++) {
  43.         std::cout << "Reading " << index << ":" << persons[index].name << std::endl;
  44.     }
  45.  
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment