1

I am having a bit of trouble on reading data in a specific format into different variables. I need to read the data from the file and store into 5 different variables of different variable types.(string Name, string Fish, string Location, float Length, float Weight)

The format of the Data I am trying to store is as follows:

Picture of How Data is Stored in File

I have no issues obtaining the name, fish, and location from the file using the getline() function. My issue comes with the length and width.

ifstream file("test.txt");

while(file.good())
{
    getline(file, Name);
    getline(file, Species);
    getline(file, Location);
    file >> Length;
    file >> Weight;

    cout << Name << "\n" << Species << "\n" <<Location << "\n" <<Length << "\n";

}

When I use the following code the output becomes wonky and it prints the data out of order after the first listing. Any help with this would be greatly appreciated.

Eric Olson
  • 11
  • 2
  • No images, please. – Evg Jan 22 '21 at 07:14
  • 2
    `while(file.good())` is wrong, see [this](https://www.tutorialspoint.com/why-is-iostream-eof-inside-a-loop-condition-considered-wrong) for explanation. – n. 'pronouns' m. Jan 22 '21 at 07:14
  • A hint: The "input" operator `>>` does *not* read the ending newline. – Some programmer dude Jan 22 '21 at 07:16
  • what do you mean by >> does not read the ending newline, I'm very new to c++ sorry – Eric Olson Jan 22 '21 at 07:18
  • At the end of each line in the input file there's a newline, right? When you do `file >> Weight;` the program reads the value into `Weight` but the newline is *not* read. It's still left in the input buffer for the next iteration and the next call to `getline` to read. And when `getline` reads if, then it stops reading. Essentially it reads an "empty" line. And if the input file really have empty lines between records, then your code will become even more confused. You could have found this out very quickly by using a *debugger* to step through the code. – Some programmer dude Jan 22 '21 at 07:21
  • This is why I always recommend that you read full lines of everything, and if needed convert the line into the correct type from the string. For example `getline(TemporaryString); Length = stoi(TemporaryString);` – Some programmer dude Jan 22 '21 at 07:23
  • 1
    The duplicate talks about input from the console, but exactly the same issue applies to files. – john Jan 22 '21 at 07:37

1 Answers1

0
ifstream file("test.txt");
while (file) {
    getline(file, Name);
    getline(file, Species);
    getline(file, Location);
    file >> Length;
    file >> Weight;

    if (file) {
       cout << Name << "\n" << Species << "\n" <<Location << "\n" <<Length << "\n";

        // add this to skip the 2 newlines before reading the next string
        file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        file.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
    }
    else {
       throw "Invalid input";
    }
}
StPiere
  • 3,910
  • 12
  • 21