I'm reading in Netflix shows from a csv file into my object array using getline. Some of the strings have two commas so when getline encounters the first one it stops halfway through the string splitting it. For example, "crude and sexual humor, language and some drug content" would be separated into "crude and sexual humor" and "language and some drug content". This affects the rest of the data as getline starts again at the wrong point in the file. Is there a way I can ignore the first comma so the full string is stored in the proper variable within the array?
These are the first three rows in the csv file:
White Chicks,PG-13,crude and sexual humor, language and some drug content,80,2004,82,80
Lucky Number Slevin,R,strong violence, sexual content and adult language,100,2006,NA,82
Grey's Anatomy,TV-14,Parents strongly cautioned. May be unsuitable for children ages 14 and under.,90,2016,98,80
This is the code in main.cpp
#include <iostream>
#include "Video.h"
using namespace std;
int main() {
Video playlist;
Video Netflixplaylist[1000];//Declare Video array
playlist.CreatePlaylist(Netflixplaylist);
cout << Netflixplaylist[0].media1.title << endl;
cout << Netflixplaylist[1].media1.title << endl;//Test if data was properly stored
return 0;
}
This is the code in Video.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
#include "Video.h"//Inlcude Video.h file
void Video::CreatePlaylist(Video playlist[1000]){
ifstream myfile;
int row = 0;
myfile.open("/Users/Auditore/Desktop/Netflix_Shows.csv");
while(!myfile.eof()){
for(row; row < 1000; row++){
getline(myfile, playlist[row].media1.title, ',');
getline(myfile, playlist[row].media1.rating, ',');
getline(myfile, playlist[row].media1.rating_lv, ',');
getline(myfile, playlist[row].media1.description, ',');
getline(myfile, playlist[row].media1.release_year, ',');
getline(myfile, playlist[row].media1.user_rate, ',');
getline(myfile, playlist[row].media1.user_rate_size, '\n');
}
}
}
This is the code in Video.h
#ifndef Video_h
#define Video_h
#include <fstream>
using namespace std;
struct MediaInfo{
string title;
string rating;
string description;
string rating_lv;
string release_year;
string user_rate;
string user_rate_size;
};
class Video {//Video class
private:
ifstream myfile;
public:
std::string playlistname = "My Bored Playlist";//Set playlistname to Boring Playlist
MediaInfo media1;
void CreatePlaylist(Video playlist[1000]);
};
#endif