1

I tried to read a file called "qbdata.txt" and save the data in a vector called quarterbacks. So, I created this struct 'Record' to save different types of variables in the file and 'quarterbacks' is supposed to be a vector of struct. Here is my code. However, it didn't work out. When I test the size of my vector, it resulted zero. Can you tell me what's wrong with my code? (I also uploaded a piece of the text file I am trying to withdraw data from)

#include <iostream>
#include <fstream>
#include <stdexcept>
#include <vector>

struct Record{
    int year;
    string name;
    string team;
    int completions, attempts, yards, touchdowns, interceptions;
    double rating;
};

void readFile()
{
    ifstream infile;
    Vector<Record> quarterbacks;

    infile.open("qbdata.txt");
    if (infile.fail()){
        throw runtime_error ("file cannot be found");
    }
    while (!infile.eof()){
        Record player;
        if (infile >> player.year >> player.name >> player.completions >> player.attempts >>
            player.yards >> player.touchdowns >> player.interceptions)
            quarterbacks.push_back(player);
        else{
            infile.clear();
            infile.ignore(100, '\n');
        }
    }
    infile.close();
}
Phyllis Qu
  • 27
  • 5
  • Probably unrelated, but will get you sooner or later: [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Feb 03 '18 at 23:23
  • Unrelated: `infile.ignore(100, '\n');` what will you do if there are more than 100 characters left on the line? – user4581301 Feb 03 '18 at 23:24
  • I think we need a [mcve] to make sure you're not doing something like testing a `quarterbacks` that's in a different scope. In the meantime, fire up the debugger, pop a breakpoint on that big long `if` and run the program. When the breakpoint is hit, step and see how much of player got the correct values. – user4581301 Feb 03 '18 at 23:30
  • Search the internet for "StackOverflow c++ read file struct". There are already a plethora of data file parsing related questions. Always search before posting. – Thomas Matthews Feb 03 '18 at 23:37
  • 1
    You may want to simplify your program by overloading `operator >>` in your class. This would make the while statement: `while (infile >> player)`. – Thomas Matthews Feb 03 '18 at 23:38
  • I guess in this case I am just assuming the user won't type more than 100 times wrong... – Phyllis Qu Feb 03 '18 at 23:43
  • I don't see the text you're trying to read. Have you considered using a debugger to step through your code? You might also consider reading each value on its own so you can see which value it is failing on. – Retired Ninja Feb 03 '18 at 23:45
  • 'year player_name team completions attempts yards touchdowns interceptions' ---- each line of the data file is formatted like this...I am trying to overload my input operator to see if it would work ...@Thomas Matthews, and searching what a debugger is lol – Phyllis Qu Feb 04 '18 at 00:04
  • You are better off reading file line and then parsing that into your struct. Having said that, does your file contain `team` information, because it seems to be missing in your struct read. – Killzone Kid Feb 04 '18 at 00:07
  • Oh my goodness! I am missing team in my struct read !!! Thank you! Btw, would overloading input operator make my code easier to read? – Phyllis Qu Feb 04 '18 at 00:33

2 Answers2

0

probably, the vector is not populated since condition is not met. the operator >> takes one word at time and ignores spaces, if you read more than in the file for example, the condition is not met.

0

Read one line at a time using std::getline, then use std::stringstream to parse the data. Example:

#include <sstream>
...
string line;
while(getline(infile, line))
{
    cout << "test... " << line << "\n";
    stringstream ss(line);
    Record player;
    if(ss >> player.year >> player.name >> player.completions >> player.attempts >>
        player.yards >> player.touchdowns >> player.interceptions)
    {
        quarterbacks.push_back(player);
        cout << "test...\n";
    }
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77