Separate String and put into Class

Hi :)
I have an assignment to create a battleship replica via code.
I'm trying to read my .csv file into my program, and I managed to remove
the commas, replacing them with a space. However, I do not know how to separate
the string and put them into my ship's objects.

The .csv files look like this
1
2
Battleship,A6,V
//Type of ship, location on grid, and vertical or horizontal. 


This is my function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void loadShip(fstream &inFile) {
    inFile.open("ship_placement.csv");
    string type, location, line, temp;
    char horVer;
    Ship ships[5];
    if (!inFile) {
        cout << "Unable to locate file.";
        exit(1);
    } else {
        for (int i = 0; i < 5; i++) {
            //getline(inFile, temp, ',');
            inFile >> temp;
            replace(temp.begin(), temp.end(), ',' , ' '), temp.end();
            //Where I'm having trouble...
            ships[i].settypeShip();
            cout << ships[i].gettypeShip();
        }
    }
}


This is my Ship class.
1
2
3
4
5
6
7
class Ship{
private:
    string typeShip;
    string location;
    char horVer;
...
}


My code outputs something like this so far because of deleting the commas.
 
Battleship A6 V

I only need to separate them and place them into my Ship class. Can someone please help me :) Thank you!
Last edited on
What's the problem with line 11?
Once you're changed the commas to spaces, the easiest way to parse each line is to use a stringstream.

10
11
12
13
14
15
16
17
18
for (int i = 0; i < 5; i++) 
    {   inFile >> temp;
        replace(temp.begin(), temp.end(), ',', ' '), temp.end();
        stringstream ss(temp);
        ss >> type >> location >> horVer;   
        ships[i].settypeShip(type);
        cout << ships[i].gettypeShip();
        //  Set the other attributes
     }

Topic archived. No new replies allowed.