Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef PLAYER_H
- #define PLAYER_H
- #include <string>
- using namespace std;
- class Player{
- private:
- int nrOfMatches;
- public:
- string firstname;
- string lastname;
- int birthyear;
- string* matchdates;
- string toString();
- void addMatchDate(string date);
- Player();
- Player(string firstName, string lastName, int birthYear);
- ~Player();
- };
- #endif
- #include "Player.h"
- #include <sstream>
- #include <string>
- using namespace std;
- Player::Player(){
- firstname = " ";
- lastname = " ";
- birthyear = 0;
- nrOfMatches = 0;
- matchdates = NULL;
- }
- Player::Player(string firstname,string lastname, int birthyear ){
- this->firstname = firstname;
- this->lastname = lastname;
- this->birthyear = birthyear;
- nrOfMatches = 0;
- matchdates = NULL;
- }
- string Player::toString(){
- string birthyearToString;
- string matchDates;
- ostringstream convert;
- convert << birthyear;
- birthyearToString = convert.str();
- for (int i = 0; i < nrOfMatches - 1; i++){
- matchDates += matchdates[nrOfMatches] + ", ";
- }
- matchDates += matchdates[nrOfMatches];
- string toStringReturn;
- toStringReturn = "Namn: " + firstname + " " + lastname + "\n" + "Födelseår: " + birthyearToString + "\n" + matchDates ;
- //string toStringReturn;
- //toStringReturn = "Namn: " + firstname + " " + lastname + "\n" + "Födelseår: " + birthyearToString;
- return toStringReturn;
- }
- void Player::addMatchDate(string date){//function for adding matches
- if (nrOfMatches >= 1){
- string* tmp;
- tmp = new string[nrOfMatches + 1];
- for (int i = 0; i < nrOfMatches; i++){
- tmp[i] = matchdates[i];
- }
- tmp[nrOfMatches] = date;
- delete[] matchdates;
- matchdates = tmp;
- }
- else{
- matchdates = new string[nrOfMatches + 1];
- matchdates[nrOfMatches] = date;
- }
- nrOfMatches++;
- }
- Player::~Player(){
- delete[] matchdates;
- }
- #include <iostream>
- #include <fstream>
- #include <string>
- #include "Player.h"
- using namespace std;
- int main(int argc, char* argv[]){
- int nrOfPlayers = 0;
- int nrOfDates = 0;
- int yearOfBirth;
- string firstname;
- string lastname;
- string matchdate;
- Player* team;
- ifstream inFile;
- inFile.open("Team.txt");
- if (inFile.fail()){
- cerr << "Error" << endl;
- exit(1);
- }
- inFile >> nrOfPlayers;
- team = new Player[nrOfPlayers];
- for (int i = 0; i < nrOfPlayers; i++){
- inFile >> firstname;
- inFile >> lastname;
- inFile >> yearOfBirth;
- inFile >> nrOfDates;
- /*Player* player;
- player = new Player(firstname, lastname, yearOfBirth);*/
- Player player(firstname, lastname, yearOfBirth);
- team[i] = player;
- cout << team[i].firstname << endl << team[i].lastname << team[i].birthyear;
- //cout << firstname + lastname << endl; yearOfBirth;
- for (int y = 0; y < nrOfDates; y++){
- inFile >> matchdate;
- team[i].addMatchDate(matchdate);
- cout << team[i].matchdates << endl;
- }
- }
- /*for (int i = 0; i < nrOfPlayers; i++){
- cout << team[i].toString();
- }*/
- /*Player* team;
- team = new Player(firstname, lastname, yearOfBirth);
- cout << team->birthyear;*/
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement