I'm having trouble with vectors. I have a class that I read from a file, then I insert it into a vector. It should work fine, but my compiler crashes every time. Am I not using the vector correctly?
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
class Movie {
private:
string movieTitle, studioName, Actors[5];
int yearMade, numOfActors;
double grossIncome;
ifstream fin;
public:
void Read(ifstream &fin){
string s;
numOfActors=0;
getline(fin, movieTitle);
fin >> yearMade;
fin >> grossIncome;
fin.ignore(1,' ');
getline(fin, studioName);
getline(fin, s);
istringstream ss(s);
for (int n=0; getline(ss, Actors[n], ','); n++){
ss >> ws;
numOfActors++;
}
}
//setter and getters
};
bool openInput(ifstream &);
int main() {
ifstream fin;
vector<Movie> M1;
Movie M2;
if (openInput(fin)) {
M2.Read(fin);
M1.push_back(M2); // CRASHES HERE-----------
fin.close();
}
cout << endl << "Program Terminated -- Have a Nice Day!" << endl << endl;
system("PAUSE");
return 0;
}//end main
bool openInput(ifstream &fin) {
string inputFileName;
cout << "Enter the input file name: ";
getline(cin,inputFileName);
fin.open(inputFileName.c_str());
if(fin.fail()) {
cout << endl << "** Bad input file name! Program terminated! **\n";
}
return (!fin.fail());
}