mercredi 21 octobre 2020

string tokeniser, delimiter and making objects with file input

I have a file that have multiple lines of the sample data given in the code. Each line is an object. I've been trying to work with string tokenizer but I keep getting errors.

#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::string input = "kevin hill;8;8;jacky knight;5;6;alejandro wilson;jordan walls;6;layla penn;7;mindy kaling;9;jon adams;8;";

    std::istringstream ss(input);
    std::string token;

    std::string mN, fN, mgr, psy, physio, tCo, fCo;
    int mSta, mStr, fSta, fStr, psyS, physioS, tCoS, fCoS;

    struct points
    {
        std::string athName, sportName;
        int totPoints, sc;
        points(int totPoints, int sc, std::string athName, std::string sportName)
        {
            this->totPoints = totPoints;
            this->sc = sc;
            this->athName = athName;
            this->sportName = sportName;
        }
    };

    while (getline(ss, token, ';'))
    {
        mN >> mSta >> mStr >> fN >> fSta >> fStr >> mgr >> psy >> psyS >> physio >> physioS >> tCo >> tCoS >> fCo >> fCoS;
    }

    points *one = new points(mSta, mStr, mN, fN);

    std::cout << one->athName << std::endl;
}

I'm getting error in the beginning of the while loop as the >> after mN is giving "no operator matches these operands" error. When I start the while loop with the istringstream ss as:

ss >> mN >> mSta >> .....

It executes, but skips the first name and reads 8;8;jacky as one string and I guess that is because it is trying to complete a string but even then it is skipping the delimiter as it stops reading at a whitespace. I'm clueless what is happening here. How do I read different data types using delimiters and make objects with them? Any suggestions?

Aucun commentaire:

Enregistrer un commentaire