0

I have a Runner class which has three member variables (firstName, lastName, and pace), and a default constructor that does nothing.

I'm trying to read from a file that stores firstName, lastName, and pace, then assign the values to a Runner object before adding it to a vector.

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "runner.h"
using namespace std;

int main(){
    ifstream input("test.txt");
    vector<Runner> list;
    string holdFN;
    string holdLN;
    int holdP;
    int x=0;
    while (x!=1){ //Shows whether opening was successful
        if (input.is_open()){
            cout << "\nFile Opened Successfully.\n\n";
            x++;
        } else {
            cout << "\nFile Failed to Open.\n\n";
        }
    }
    while (input.eof()!=1){
        Runner runner;
        input >> holdFN >> holdLN >> holdP;
        runner.firstName = holdFN;
        runner.lastName = holdLN;
        runner.pace = holdP;
        list.push_back(runner);
    }
    for (int i=0; i<list.size(); i++){ //Testing to see if vectors stored correctly
        cout << list[i].pace << " ";
    }

    return 0;
}

When I don't involve a while loop and just have the loop's "guts", the code works and I get a successful output of the object's pace from the vector.

However, when I run the code as it currently is above, I get the following output:

File Opened Successfully.
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

I'm not sure how to fix this. Does anyone have any ideas?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    `while (input.eof()!=1)`: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) How this hurts you: Say there's an error reading the input and the stream is placed into the fail state. Only EOF is ever checked for, but it can never be reached. The program will keep making more `runners` based on garbage data and stuff them into `list` until `list` runs the program out of memory. – user4581301 May 05 '22 at 22:40
  • On a separate note, if the file fails to open, the 1st `while` gets stuck in an endless loop, since you don't try to re-open the file and update `x` when successful. – Remy Lebeau May 05 '22 at 22:44
  • @user4581301 That makes sense. How should I best combat this then? I'm not quite sure I understand the answers in the link you've given me. Thanks! – Eccentric Tuber May 05 '22 at 22:44
  • @EccentricTuber change `while (input.eof()!=1){ Runner runner; input >> holdFN >> holdLN >> holdP; ... }` to `while (input >> holdFN >> holdLN >> holdP){ Runner runner; ... }` instead. – Remy Lebeau May 05 '22 at 22:45
  • Would I then delete that line from inside of the loop as well? Forgive me, I'm new at this. – Eccentric Tuber May 05 '22 at 22:46
  • Yes remove the `input >> holdFN >> holdLN >> holdP;` line and change the `while ()` – drescherjm May 05 '22 at 22:46
  • 1
    `Runner runner; while (input >> runner.firstName>> runner.lastName>> runner.pace) { list.push_back(runner); }` Read until a read fails for any reason (might as well read directly into `runner`) if the reads don't fail, store the `runner`. This catches file not open, file read error and end of file. Pretty much all the bases are covered. – user4581301 May 05 '22 at 22:47
  • @EccentricTuber "*Would I then delete that line from inside of the loop as well?*" - obviously, yes. Otherwise, if you read a runner from inside the `while` statement itself, and read again inside the loop body, then you would be reading and discarding the *next* runner in the file. You want to read only 1 runner per iteration. – Remy Lebeau May 05 '22 at 22:48
  • Thank you all for the help! It's now working, and I really appreciate it. – Eccentric Tuber May 05 '22 at 22:49
  • @RemyLebeau I do apologize for not seeing the obvious, but I still am grateful for your help. – Eccentric Tuber May 05 '22 at 22:50
  • just so you get why you got that error. That while loop never exits, so you keep making Runner objects, filling them with junk and then pushing into that vector. This keeps going till you run out of memory – pm100 May 05 '22 at 22:51

0 Answers0