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.
For example:
If the file contains,
HELLO
GOODBYE
STOP
Then what is displayed is:
HELLO
GOODBYE
STOPP
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
char charInFile;
// Loop until end of file
while (!cinfile.eof())
{
// Store selected char into charInFile
cinfile.get(charInFile);
// If blank, print new line
if(charInFile == ' ')
cout << endl;
// Else, print the character
else
cout << charInFile;
} // End while
} // End PrintFile()