This function displays to the screen the contents (words separated by spaces) of a text file. It gets a character and checks if it is a blank space. If it is, a newline is displayed. If not, the character is displayed. This continues until the eof is encountered.
The problem I have is that the last letter of the last word in the file is always doubled. For example, if the file contains the word "HELLO", then "HELLOO" is what is displayed.
I've checked the file itself and it does contain the correct spelling of the word, only the getting or displaying seems to be the problem.
Also, only the last word of a file is affected.
If the file contains,
Then what is displayed is:
Maybe I need a cin.ignore() statement before the cout using eof as a delimiter but I'm not sure how.
Thanks in advance!!!
Here is the function:
// Prints every word from already open file stream
void PrintFile(ifstream& cinfile)
// To hold current character
// Loop until end of file
// Store selected char into charInFile
// If blank, print new line
if(charInFile == ' ')
cout << endl;
// Else, print the character
cout << charInFile;
} // End while
} // End PrintFile()