Hey. I tried to figure this out but I'm just not having making any progress. My class requires me to make a program that reads characters from a .txt file and state how many characters are in the file as well as how many words are in the file. I am able to correctly assess the amount of printed characters, but the amount of words continually tells me a different answer than it's supposed to be. Thanks for your help.
int characterCountFunct(ifstream &input, int &wordCount)
{
// Declares a variable which counts the amounts of characters
int charCount = 0;
// A variable that analyzes the characters passed through it from the document
char character;
// Declares a variable which counts the amount of words in the document
// which is a reference variable manipulating a variable from the main function
wordCount=0;
// As long as the input has a character to analyze
while (input.get(character))
{
// If the character being analyzed is NOT a new line, tab, return or empty space
if (isspace(character))
{
// Then it must be a character, increase the count by one.
wordCount++;
}
// Otherwise tell the user what issue is being found.
elseif (!isspace(character))
{
charCount++;
}
elseif (input.eof())
{
wordCount++;
}
}
return charCount;
}