0

I'm trying to read from a file and am able to do so successfully, but the isstream::get() function keeps skipping the first character. I read online it's because the cin usually makes the buffer skip the first character, but I am not sure how to fix it on my end.

Here is my main:

int main () {

string fileName;

cout << "Please enter the name of the input file: \n";
cin >> fileName;

//Creates an object and executes appropriate functions
pa3* obj = new pa3(fileName);
obj->seperateCode();
obj->printFields();

return 0;
}

And here is the constructor

 //Constructor
    pa3 (string fileName) {

        char word;
        string temp;

        ifstream userFile;

        string fullFileName = "\"" + fileName + "\"";

        userFile.open(fileName);

        if (!(userFile.is_open())) {
            cout << "Not a valid file\n";
            cout << "Exiting...\n";
            exit(0);
        }
        else {

          //  userFile.unget();

            while (!userFile.eof()) {

                userFile.get(word);

                temp += word;

                if (isspace(word)) {

                    stackImplementation::push(temp, totalFile);
                    temp.clear();

                    }

            }
        }

        for (vector<string>::iterator it = totalFile.begin(); it != totalFile.end(); ++it) {
            cout << *it << " ";
        }
    }

I have tried searching online but cannot apply the solutions to my issue, since most use cin and ignore to get rid of the problem. Any help would be appreciated.

0 Answers0