-
June 11th, 2011, 06:02 AM
#1
how to count words in a text file
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;
}
Last edited by katy_price; June 11th, 2011 at 06:04 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|