-2

I am trying to figure out why I am only getting one line while using getline from the file. I am using reverse to flip all of the letters then turning the string into a stringstream. I am then pulling each word out of the stream and reversing them in the order I extracted them. So if I am am pulling the line "This is a line", the output is "line a is This". My problem is that it is only getting the first line and then stopping. I thought it had to do with the newline character, but I tried to use .ignore to no avail. I've also tried it without .ignore. Any help would be appreciated.

int main()
{
        fstream inFile;
        fstream outFile;
        string fileName("");
        string destName("");
        char c = 0;
        string wrdRev("");
        int numCount = 0;
        int capCount = 0;
        int lowCount = 0;
        int wordCount = 0;
        int charCount = 0;
        string buffer("");
        istringstream strInput;
        string output("");
        string lineRev("");

        cout << "Please enter file name: ";
        getline(cin, fileName);
        cout << endl;

        inFile.open(fileName, ios::in);

        if (inFile.good() != true) {
                cout << "File does not exist!\n" << endl;
                return 0;
        }
        else{
                reverse(fileName.begin(), fileName.end());
                destName += fileName;
        }


        outFile.open(destName, ios::in);

        if (outFile.good() == true){
                cout << "File '" << destName << "' already exists!\n" << endl;
                return 0;
        }
        else {
                outFile.clear();
 outFile.open(destName, ios::out);


                while(inFile.get(c)){

                        if(isupper(c)){
                                capCount++;
                        }
                        else if(islower(c)){
                                lowCount++;
                        }
                        else if(isdigit(c)){
                                numCount++;
                        }
                        else if(isspace(c)){
                                wordCount++;
                        }
                }

                inFile.clear();
                inFile.seekg(0, inFile.beg);

                while(inFile >> buffer){
                        charCount = charCount + buffer.length();
                }

                inFile.clear();
                inFile.seekg(0, inFile.beg);

                while(getline(inFile, wrdRev)){
                        inFile.clear();
                        reverse(wrdRev.begin(), wrdRev.end());

                        strInput.str(wrdRev);

                        while(strInput >> output){
                                strInput.ignore();
                                reverse(output.begin(), output.end());
                                cout << output << " ";
                        }
                        inFile.clear();
                        inFile.ignore(8192, '\n');
                        cout << endl;
                }

                outFile << "There are " << capCount << " upper-case letters." << endl;
                outFile << "There are " << lowCount << " lower-case letters." << endl;
                outFile << "There are " << numCount << " digits." << endl;
outFile << "There are " << charCount << " characters." << endl;
                outFile << "There are " << wordCount << " words.\n" << endl;

        }

        inFile.close();
        outFile.close();

        return 0;


}
ryanpback
  • 275
  • 4
  • 17

1 Answers1

0

I was able to solve the issue by clearing the stringstream and starting the position back at the beginning.

while(getline(inFile, wrdRev)){
    inFile.clear();
    reverse(wrdRev.begin(), wrdRev.end());
    strInput.str(wrdRev);
    strInput.clear();
    strInput.seekg(0);

    while(strInput >> output){
        strInput.ignore();
        reverse(output.begin(), output.end());
        outFile << output << " ";
    }           
}
ryanpback
  • 275
  • 4
  • 17