Counting words in a text file

I am near complete of my program. The only element that does not produce is the word counter. I believe my code is incremented correctly and would like a second opinion. The output I receive for the word count is always 0, which it shouldn't be since I have a text file filled with lines of words. Is it only incrementing that is needed in this IF statement?


while (!inFile.eof())
{
getline(inFile, lineOfText, '\n');
nmbrOfLines++;

int len = lineOfText.length();
string prevChar = "";
for (int x = 0; x <= len; x++)
{
string currentChar = lineOfText.substr(x,1);
if(currentChar == ";")
{
nmbrOfSemi++;
}
else if(currentChar == ".")
{
nmbrOfPeriods++;
}
else if(currentChar == ",")
{
nmbrOfCommas++;
}
else if(currentChar == "" && prevChar != "") //Word count outputs always "0"~
{
nmbrOfWords++;
}
currentChar = prevChar;

}
}
Last edited on
1
2
3
4
5
6
7
///else if(currentChar == "" && prevChar != "") //Word count outputs always "0"~
else if(currentChar == " " && prevChar != " ") //Word count outputs always "0"~
{
nmbrOfWords++;
}
///currentChar = prevChar;
prevChar = currentChar;


This may help you.
Sure did thanks~
Topic archived. No new replies allowed.