Tim's
Tim's

Reputation: 246

How to convert this vector code into a class?

I was trying to put this code into a class but I couldn't manage to do it. The job of the function is pulling team names from a .txt file and putting them in a vector. I think the main problem is I couldn't select the right function return type.

This is the teams.txt: (The names before the "-" symbol are teams. Other names are unrelated with my question but they are coachs of the teams.)

Trabzonspor-Abdullah Avcı+
Fenerbahçe-Vítor Pereira+
Beşiktaş-Sergen Yalçın+
Galatasaray-Fatih Terim+
İstanbul Başakşehir-Emre Belözeoğlu+
Alanyaspor-Bülent Korkmaz+
Fatih Karagümrük-Francesco Farioli+
Gaziantep-Erol Bulut+
Adana Demirspor-Vincenzo Montella+
Ankara Dinc-Nestor El Maestro+
Antalyaspor-Nuri Şahin+
Kayserispor-Hikmet Karaman+
Yeni Malatyaspor-Marius Sumudica+
Konyaspor-İlhan Palut+
Sivasspor-Rıza Çalımbay+
Hatayspor-Ömer Erdoğan+
Giresunspor-Hakan Keleş+
Kasımpaşa-Hakan Kutlu+

And this is the my code who does the putting them in a vector:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std; //I know that's a bad practice but i just need to do this for while

std::string process(std::string const& s) //A function to seperate teams from the coaches
{
    string::size_type pos = s.find('-');
    if (pos != string::npos)
    {
        return s.substr(0, pos);
    }
    else
    {
        return s;
    }
}


int main() {

    ifstream readTeam("teams.txt");
    if (!readTeam) {  //checking that successfully opened the file.
        std::cerr << "Error while opening the file.\n";
        return 1;
    }
    vector<std::string> teams;
    string team;
    while (getline(readTeam, team)) {
        teams.push_back(process(team));
    }
    readTeam.close();

    int g = 1;//for printing the teams, just for displaying it. doesn't have to in a class.
    for (const auto& i : teams) {
        cout << g;
        cout << i << endl;
        g++;
    }

    return 0;
}

And that's what i did(tried) to make it a class:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

std::string process(std::string const& s)
{
    string::size_type pos = s.find('-');
    if (pos != string::npos)
    {
        return s.substr(0, pos);
    }
    else
    {
        return s;
    }
}


class readFile {
public:
    void setTxtName(string);
    vector<unsigned char> const& getTeam() const{
    }
    vector<string> teams;

private:
    string fileName;
    
};

int main() {
    
    readFile pullTeams;

    pullTeams.setTxtName("teams.txt");
    

    return 0;
}

void readFile::setTxtName(string txtName) {
    fileName = txtName;
}

 
vector<string> const& readFile::getTeam { //problem is defining it(I think). So I couldn't add my main code int it..
    
    return teams;
}

Anything helps, thank you!

Upvotes: 0

Views: 86

Answers (1)

Tim&#39;s
Tim&#39;s

Reputation: 246

I did a little different research based on the Botje's comment. And I manage to create an answer based on here. Thanks.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;


std::string process(std::string const& s){
    string::size_type pos = s.find('-');
    if (pos != string::npos){
        return s.substr(0, pos);
    }
    else{
        return s;
    }
}


class readFile {
public:
    vector<string> getTxt();
    bool read(string);

private:
    vector<string> teams;
    string team;
    ifstream txt;
};

int main() {
    
    vector<string> teams;

    readFile team;
    if (team.read("teaams.txt") == true)
        teams = team.getTxt();

    int g = 1;//for printing the teams, just for displaying it. doesn't have to in a class.
    for (const auto& i : teams) {
        cout << g;
        cout << i << endl;
        g++;
    }
    

    return 0;
}


bool readFile::read(string txtName) {
    ifstream txt;
    string team;

    txt.open(txtName.c_str());

    if (!txt.is_open())
        return false;
    while (getline(txt, team))
        teams.push_back(process(team));
    return true;
 }

vector<string> readFile::getTxt() {
    return teams;
}

Upvotes: 3

Related Questions