jeudi 23 avril 2020

Used .ignore('\n') and the Getline function that follows is only taking half of the input from file

I tried to make sure what I'm asking wasn't a duplicate since this must be a very beginner mistake but I couldn't find something similar but if someone has a reference to a similar post that'd be great as well anyways So I'm trying to make a battleship game that reads in the ship placement from a .csv file but before it places the ships it goes through the file and makes sure all the ships are found in the file.

The data in the file is formatted as follows:

Carrier,B2,H
Battleship,D4,V
Cruiser,G4,H
Submarine,G5,V
Destroyer,G8,H

The part that is causing me issues in my code is this block:

cout << "File successfully located!\n Making sure all ships are in file...\n";
while (shipPlacement.good()) {
        getline(shipPlacement, shipType, ',');
        cout << shipType << endl;
        shipPlacement.ignore('\n');
    }

I want it to only take in the battleship type and ignore everything else in the file. The first line is read in correctly it stops reading until the set delimiter, great! and then I want it to ignore everything that follows until the next line so I put shipPlacement.ignore('\n') and from that point it should loop again reading in the next ship up until the delimiter, etc. am I missing something here? what is happening is only half of the ship type is getting taken as input. This is the output I'm getting:

Making sure all ships are found in file...
Carrier
leship
ser
arine
royer

This is probably a simple fix but I can't see what's in front of me it looks like, Any help or guidance is appreciated!

Aucun commentaire:

Enregistrer un commentaire