0

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?

cigien
  • 57,834
  • 11
  • 73
  • 112
wakanada
  • 115
  • 1
  • 9
  • Does this answer your question? [Reading formatted data with C++'s stream operator >> when data has spaces](https://stackoverflow.com/questions/2338827/reading-formatted-data-with-cs-stream-operator-when-data-has-spaces) – AVH Oct 21 '20 at 14:56
  • By using `;` as a `std::getline` delimiter, this places, initially "kevin hill" into `token`, on the next iteration it places "8" into `token`, and so on. Remember The Golden Rule Of Computer Programming: your computer always does exactly what you tell it to do instead of what you want it to do. That's what you told your computer to do, so that's what it did. Now, reread that loop and see if anything makes sense to you? – Sam Varshavchik Oct 21 '20 at 14:56
  • @SamVarshavchik yes, I do understand what you're pointing at, but that way it's too many lines of code. Like java, isn't there a nextToken function in cpp. Like I was trying for object with 4 lines and it's this much: getline(ss, mN, ';'); getline(ss, n, ';'); mSta = stoi(n); getline(ss, n, ';'); mStr = stoi(n); getline(ss, fN, ';'); – wakanada Oct 21 '20 at 18:22
  • C++ is not Java. C++ works in fundamentally different ways from Java, and attempting to analogize code between Java and C++ will only end in tears. The C++ way to parse something like this would be to use `std::getline` to read the ***entire*** line into a `std::string`, then use iterators, algorithms, and containers to parse it out. It should take only a few lines of code to take a single string, and break it up into individual strings, delimited by semicolons. There is no discrete "nextToken" in C++, this is implemented using generic C++ library algorithms, iterators, and containers. – Sam Varshavchik Oct 21 '20 at 19:07

0 Answers0