By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,396 Members | 2,501 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,396 IT Pros & Developers. It's quick & easy.

questions.open("help.me"

P: n/a
the assignment is to count the number of words in a txt file here is my code

#include <iostream>
#include <fstream>
#include <cassert>

using namespace std;

int main ()
{
string readIn;
string fileName;
int wordCount;
ifstream inFile;

wordCount = 0;

while (fileName != "quit")
{
cout << "enter file name"<<endl;
cin >> fileName;

inFile.open (fileName.c_str());

assert(inFile);

while (inFile)
{
inFile >> readIn;
wordCount++;

}

}
cout << wordCount;

return 0;
}

and here is the file

This &%file should!!,...

have exactly 7 words.
a word is defined byu any non-white space. every time i run it it gives me 8
instead
of 7, what am i doin wrong? please help, thanks/


Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Luis" <ki***@comcast.net> wrote in message
news:Jx1Pa.22204$ye4.17360@sccrnsc01...
the assignment is to count the number of words in a txt file here is my
code

[ . . . ]

while (inFile)
{
inFile >> readIn;
wordCount++;
}

[ . . . ]

every time i run it it gives me 8 instead of 7, what am i doin wrong?


The loop doesn't stop until inFile's fail bit is set. But the fail bit
isn't set until after you attempt to read past the end of the file. So,
what happens is this:

1. You read 7 words, and wordCount is incremented 7 times.
2. The loop condition is tested. inFile's fail bit hasn't yet been set, so
the condition is true. The loop continues.
3. You attempt to read again. This time the read fails, and inFile's fail
bit is set.
4. wordCount is incremented again, making it 8.
5. The loop condition is tested again. This time inFile's fail bit is set,
so the condition is false, and the loop ends.

You need to make sure wordCount isn't incremented if the previous read
attempt failed. Do this:

while (inFile >> readIn)
{
++wordCount;
}

The result of the >> operation is a reference to the stream, inFile. So
inFile is tested immediately after the read operation. If the read fails,
the loop stops.

Regards,

Russell Hanneken
rh*******@pobox.com
Jul 19 '05 #2

P: n/a


Luis wrote:

a word is defined byu any non-white space. every time i run it it gives me 8
instead
of 7, what am i doin wrong? please help, thanks/


What you did wrong in the first place is:
you did not check, *what* your program reads by immediatly
outputing it to cout. If you had done this, you would have
noticed that the last 'word' is counted twice. Of why
this is going to happen, see Russells excellent reply of
how to create a reading loop that works correctly.
If your program doesn't do what you expect it to do, then
by all means start monitoring the variables. If that
means: output them to cout in order to see how they change,
then so be it.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.