Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1.  
  2. #ifndef PLAYER_H
  3. #define PLAYER_H
  4. #include <string>
  5. using namespace std;
  6.  
  7. class Player{
  8. private:
  9.     int nrOfMatches;
  10. public:
  11.     string firstname;
  12.     string lastname;
  13.     int birthyear;
  14.     string* matchdates;
  15.     string toString();
  16.     void addMatchDate(string date);
  17.     Player();
  18.     Player(string firstName, string lastName, int birthYear);
  19.     ~Player();
  20.  
  21.  
  22. };
  23.  
  24.  
  25. #endif
  26.  
  27.  
  28.  
  29.  
  30. #include "Player.h"
  31. #include <sstream>
  32. #include <string>
  33. using namespace std;
  34.  
  35. Player::Player(){
  36.     firstname = " ";
  37.     lastname = " ";
  38.     birthyear = 0;
  39.     nrOfMatches = 0;
  40.     matchdates = NULL;
  41. }
  42.  
  43. Player::Player(string firstname,string lastname, int birthyear  ){
  44.     this->firstname = firstname;
  45.     this->lastname = lastname;
  46.     this->birthyear = birthyear;
  47.     nrOfMatches = 0;
  48.     matchdates = NULL;
  49.  
  50. }
  51.  
  52. string Player::toString(){
  53.     string birthyearToString;
  54.     string matchDates;
  55.     ostringstream convert;
  56.     convert << birthyear;
  57.     birthyearToString = convert.str();
  58.     for (int i = 0; i < nrOfMatches - 1; i++){
  59.         matchDates += matchdates[nrOfMatches] + ", ";
  60.     }
  61.     matchDates += matchdates[nrOfMatches];
  62.     string toStringReturn;
  63.     toStringReturn = "Namn: " + firstname + " " + lastname + "\n" + "Födelseår: " + birthyearToString + "\n" + matchDates ;
  64.     //string toStringReturn;
  65.     //toStringReturn = "Namn: " + firstname + " " + lastname + "\n" + "Födelseår: " + birthyearToString;
  66.     return toStringReturn;
  67.  
  68. }
  69.  
  70. void Player::addMatchDate(string date){//function for adding matches
  71.    
  72.    
  73.     if (nrOfMatches >= 1){
  74.        
  75.        
  76.         string* tmp;
  77.         tmp = new string[nrOfMatches + 1];
  78.  
  79.         for (int i = 0; i < nrOfMatches; i++){
  80.             tmp[i] = matchdates[i];
  81.         }
  82.         tmp[nrOfMatches] = date;
  83.         delete[] matchdates;
  84.         matchdates = tmp;
  85.        
  86.     }
  87.     else{
  88.         matchdates = new string[nrOfMatches + 1];
  89.         matchdates[nrOfMatches] = date;
  90.        
  91.     }
  92.    
  93.     nrOfMatches++;
  94. }
  95.  
  96. Player::~Player(){
  97.     delete[] matchdates;
  98. }
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. #include <iostream>
  113. #include <fstream>
  114. #include <string>
  115. #include "Player.h"
  116. using namespace std;
  117. int main(int argc, char* argv[]){
  118.    
  119.     int nrOfPlayers = 0;
  120.     int nrOfDates = 0;
  121.  
  122.     int yearOfBirth;
  123.     string firstname;
  124.     string lastname;
  125.     string matchdate;
  126.     Player* team;
  127.  
  128.     ifstream inFile;
  129.     inFile.open("Team.txt");
  130.    
  131.     if (inFile.fail()){
  132.         cerr << "Error" << endl;
  133.         exit(1);
  134.  
  135.     }
  136.  
  137.     inFile >> nrOfPlayers;
  138.     team = new Player[nrOfPlayers];
  139.     for (int i = 0; i < nrOfPlayers; i++){
  140.         inFile >> firstname;
  141.         inFile >> lastname;
  142.         inFile >> yearOfBirth;
  143.         inFile >> nrOfDates;
  144.         /*Player* player;
  145.         player = new Player(firstname, lastname, yearOfBirth);*/
  146.         Player player(firstname, lastname, yearOfBirth);
  147.         team[i] = player;
  148.         cout << team[i].firstname << endl << team[i].lastname << team[i].birthyear;
  149.         //cout << firstname + lastname << endl; yearOfBirth;
  150.         for (int y = 0; y < nrOfDates; y++){
  151.             inFile >> matchdate;
  152.             team[i].addMatchDate(matchdate);
  153.             cout << team[i].matchdates << endl;
  154.  
  155.         }
  156.        
  157.     }
  158.  
  159.     /*for (int i = 0; i < nrOfPlayers; i++){
  160.         cout << team[i].toString();
  161.     }*/
  162.  
  163.     /*Player* team;
  164.     team = new Player(firstname, lastname, yearOfBirth);
  165.     cout << team->birthyear;*/
  166.    
  167.     system("pause");
  168.     return 0;
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement