-2

I am trying to read data from a file in which the data is semicolon delimited. Example:

The Lord of the Rings: The Fellowship of the Ring;Jackson, Peter;2001;178;93000000;871530324

Above is all one entry. There is also a number in the first line of the file stating the number of entries. Each entry is on a different line. I am trying to read in this data to a vector of structs called info. The struct is named MovieData.

vector<MovieData> readFile(string fName,MovieData info, int& numOfMovies){
    vector<MovieData> movies;
    ifstream fin;
    fin.open(fName);

    fin >> numOfMovies;
    cout << "Number of Movies: " << numOfMovies << "\n";

    for(int i=0;i<numOfMovies;i++){
        getline(fin, info.Title, ';');
        getline(fin,info.Dir, ';');
        getline(fin,info.year, ';');
        getline(fin,info.time, ';');
        getline(fin,info.cost, ';');
        getline(fin, info.revenue);
        info.profit = info.revenue - info.cost;
        movies.push_back(info);
    }

    return movies;
}

I am having trouble with reading in the data from the file using getline(), but I believe I have everything else correct.

zam1205
  • 7
  • 2
  • What is the problem you're having? It's likely the common problem of mixing formatted input and getline. After you read the number you've left the end of line character in the buffer. You most likely want to ignore that so you start reading on the next line. – Retired Ninja Dec 06 '17 at 04:47
  • I just can't get it to compile. – zam1205 Dec 06 '17 at 04:52
  • Do you think that maybe the compile error you're receiving would help? Here's a hint, you can't subtract strings. – Retired Ninja Dec 06 '17 at 04:54
  • Possible duplicate of [Reading Comma Delimited Text File Into Array](https://stackoverflow.com/questions/36849875/reading-comma-delimited-text-file-into-array) – Pouyan Dec 06 '17 at 04:55
  • After you fix your compile error, you'll need this too: https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction – Retired Ninja Dec 06 '17 at 04:58
  • Once you get it working ask for a code review here: [codereview](https://codereview.stackexchange.com/) – Martin York Dec 06 '17 at 05:25

1 Answers1

-1

I Figured it out. Changed some stuff and got everything to work. Thanks for your help.

vector<MovieData> readFile(string fName, struct MovieData, int& numOfMovies){
    vector<MovieData> movies;
    ifstream fin;
    fin.open(fName);

    fin >> numOfMovies;
    cout << "Number of Movies: " << numOfMovies << "\n";

    for(int i=0;i<numOfMovies;i++){
        movies.push_back(MovieData());
        getline(fin, movies[i].Title, ';');
        getline(fin,movies[i].Dir, ';');
        fin >> movies[i].year;
        fin.ignore();
        fin >> movies[i].time;
        fin.ignore();
        fin >> movies[i].cost;
        fin.ignore();
        fin >> movies[i].revenue;
        fin.ignore();
        fin >> movies[i].profit;
        movies[i].profit = movies[i].revenue - movies[i].cost;

    }

    return movies;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
zam1205
  • 7
  • 2