0
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

class Movie
{
private:
    ifstream inMovie;       //private member variables

    void openFile();
    void testFile();
    void readFile();

public:
    Movie();                //constructor
    void driver()
    {
        openFile();

    };          
};

void Movie::openFile()
{
    ifstream inMovie;
    inMovie.open("SciFiMovies.txt");
    testFile();
    readFile();
}

void Movie::testFile()
{
    ifstream inMovie;
    if (!inMovie.open)
    {
        cout << "Unable to open the file.";
        exit(1);
    }
}

void Movie::readFile()
{
    ifstream inMovie;
    string file;
        while (!inMovie.eof)
        {
            getline(inMovie, file);
            cout << file;
        }
}

int main()
{
    Movie movObj;
    movObj.driver();
    system("pause");
    return 0;
}

Why am I getting an error code? Using the ! typically works for me when I want to test if a file fails to open or when I read a file. Also, the teacher requires that we use several different void functions, which is why the program may look a little weird (with several functions to accomplish a simple task). Any help would be greatly appreciated, thank you.

Mason
  • 1
  • 4
  • Are you getting compiler errors? Please post the exact text of the compiler messages. – cigien Jul 09 '20 at 18:28
  • Remember that not everyone here has Visual Studio or is sufficiently familiar with Visual Studio to know what the error codes are. Side note: Wen parsing in the error messages, prefer to use the full build output from the Output tab rather than the abbreviated version in the Error list Tab. Quite often the extra information on the Output tab alone is enough to riddle out the problem without outside assistance. Fr me the error list is useful as an overview. As a problem-solving tool, it sucks. – user4581301 Jul 09 '20 at 18:35

1 Answers1

3

You have:

if (!inMovie.open)

and

while (!inMovie.eof)

Both open and eof are functions, not data members. You need to include () on them, like so:

if (!inMovie.open())

and

while (!inMovie.eof())

Also, some suggested reading: Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?

Also also, not related to the error, but since you redefine inMovie in each function, you will end up with both of those checks always being false. Remove the redefinition, and rely on the class member variable.

ChrisMM
  • 8,448
  • 13
  • 29
  • 48