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());
}

When in doubt, you should always remove the user input stuff. There is nothing wrong with your use of the Movie vector (except the name - you should call the vector Movies and the individual movie Movie, not M1 and M2):

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Movie
{
    private:
         string movieTitle, studioName;
         string Actors[2];
         int yearMade, numOfActors;
         double grossIncome;

    public:

    void SetDemoProperties()
    {
      movieTitle = "TestTitle";
      studioName = "TestStudio";
      Actors[0] = "David";
      Actors[1] = "Joe";
      yearMade = 2000;
      numOfActors = 5;
      grossIncome = 1000;
    }

};

int main()
{

  vector<Movie> M1;
  Movie M2;

  
  M2.SetDemoProperties();
  M1.push_back(M2);
  return 0;
}

You must be doing something wrong in the Read function.

David

I know that the file opening stuff works because I've used it before. And the read function worked when the program used a class array instead of a vector. Any other suggestions?

Just an aside, you shouldn't use .fail(), use .is_open()

.fail() is for checking when a stream reads something it wasn't supposed to (like trying to read a string into an integer).

There's one way to find out exactly where your program is failing, run it in a debugger and single step through your method.

I took the "ifstream fin" out of the private part of the class and it works. Know idea why, but at least it's solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.