Click here to Skip to main content
15,613,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey I am trying to read file that contains

Boss,        level,    Specials,
"FireMen",   3,        "Flame,Thrower", Fire,Breath
"Medusa",    6,        "Snake,Poison"
"Hype",      10,       "One,punch,Knock", Fly,Kick, "Dragon,Breath"

I am trying to read it into class with objects boss, level and specials

I am having problem reading from file since I split each by words by comma but it read specials like Flame,Thrower as separate due to comma in between them. How can I combine specials rather then splitting by commas so that it read Flame Thrower rather then Flame and thrower separately.
Also some specials are in quotation others are not.

I have been stuck at reading this rather complicated file. If anyone has time to read through my code and fix 10s of errors that I am getting would greatly be appreciated, Thank You

(What I did doesn't make much sense since this is my one month into C++ so still newbie and progressing)

What I have tried:

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iomanip>
#include "Boss.h"

using namespace std;
vector <Boss*> Info;

Boss* parseLine(string str)
    vector<string> store;
    string smt = " ";
    Boss* values = nullptr;
    for (int i = 0; i < (int)str.size(); i++)
        char ch = str[i];
        if (ch != ',')
            smt = smt + ch;
            smt = "";
    if (smt != "")
    values = new Boss(store[0], stoi(store[1]), store[2]);
    /*Name = store[0];
    Level = store[1];
    Specials = store[2];
    return values;
bool readFile()
    std::ifstream myFile("Bosses.txt");
    if (!myFile.is_open())
        cout << "fAILED" << "\n";
        return false;
    string str;

    int i = 0;
    while (std::getline(myFile, str))
        cout << str << endl;
        if (str[0] != '/')
            Boss* Boss = parseLine(str);
    return true;
int main()
    std::cout << "Read file\n;";
    bool reads = readFile();

    for (Boss* t : result) delete t;

    if (reads)
        std::cout << "All good\n";

#include <string>
#include <vector>

class Boss {
	std::string Name;
	int Level;
	std::vector<std::string> Specials;
    Boss(std::string Name, int Level, std::vector<std::string> Specials);
    Boss(Boss& b);
    void setName(std::string Name);
    void setLevel(int Level);

Boss::Boss(std::string Name, int Level, std::vector<std::string> Specials)
    this->Name= Name;
    this->Level = Level;
    this->Specials = Specials;

Boss::~Boss() {} 

Boss::Boss(Boss& b)
    Name = b.Name;
    Level = b.Level;
    Specials = b.Specials;
void Boss::setName(std::string Name) {
    this->Name = Name;
void Boss::setLevel(int Level)
    this->Level = Level;
Updated 12-Apr-21 6:05am

1 solution

It looks like the words to be grouped together are enclosed in quotation marks, so you'll have to add that to your parsing logic. When parsing within quotation marks, you'll have to get the next word and add it to the one you're working on after separating it with a space.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900