hi i have this file with words printed on seperate lines. i want to count these words and display the total on the screen. at present i have a function to do this however it doesn't seem to be working, it outputs to screen:
word count: 0.
word count: 0.
word count: 0. repeatedly on seperate rows always saying 0, when basically i want it to say for example, word count: 50 just one time as opposed to 50 times can anyone help me figure out why its not doing that. thanks.
Code:int wordcount(){ ifstream inFile; inFile.open("C:\\Users\\hp\\Desktop\\egg.txt"); if (inFile.is_open()) { string word; unsigned long wordCount = 0; while(!inFile.eof()) { inFile >> word; if(word.length() > 0) { wordCount++; } } cout << "Unique word count: " << wordCount << endl; } return 0; }


Reply With Quote
Bookmarks