-4

In my code below errors occur and the program will not run, I am required to make a Constructor that must open the file with the given filename. If the filename does not exist then it Prints an error message and terminates the program.

Below is the code that I have done so far in C++:

#include "ReadWords.h"
#include <iostream>
#include <cstdlib>

using namespace std;

ReadWords::ReadWords(const char filename[])
{
    wordfile.open(filename);
    if (!wordfile)
    {
        cout << "cannot make " << filename << endl;
        exit(1);
    }
}

void ReadWords::close()
{
    wordfile.close();
}
juzzlin
  • 45,029
  • 5
  • 38
  • 50
david98
  • 1
  • 3

2 Answers2

0

Why dont you try including fstream to the top of your file and see if that works

OneTrueSen
  • 13
  • 5
0

I suppose wordfile is of type std::fstream. If your ReadWords.h #includes <fstream>, it should work (compiles and works as expected).

By the way, it's a bad practice to use using namespace std;.

Also, since you use C++, take a look at std::string. It's safer than using plain char* or char[].

Community
  • 1
  • 1
Zereges
  • 5,139
  • 1
  • 25
  • 49