agent mike wrote:
I am trying to count words in a text file. I am using the following code:
in_stream.get(c);
if(c == ' ' || c == '.' || c == ',')
word_count++;
and the word count is too low. If I include " .... || c == '\n'
the word count is too high as it counts returns of blank lines
as a word.
I am at my wits end. I'm in a beginning c++ class and this assignment
is nearly due.
Hint: Often it is a good idea to include additional output statements to figure
out why a program behaves the way it does. So I suggest:
in_stream.get(c);
cout << "Read '" << c << "'\n";
if(c == ' ' || c == '.' || c == ',') {
cout << "Found new word"\n";
word_count++;
}
Run your program again on a faulty input that is not to large
and use the additional output to analyse why your program doesn't
count the way you think it should.
Thats one of the simplest debug technique and believe me: you will
spend lots of time in debugging. So learning how to figure out where
your thinking was flawed is something you need to learn early.
And of course its fun: You think of a system (an algorithm) and
the machine shows you where your system breaks down.
--
Karl Heinz Buchegger
kb******@gascad.at