473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

questions.open("help.me"

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
2 2147
"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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: python-help-bounces | last post by:
Your message for python-help@python.org, the Python programming language assistance line, has been received and is being delivered. This automated response is sent to those of you new to...
4
by: Otis Hunter | last post by:
I have been fighting with this for days and your expert help is needed! Below is the code I am executing which results with "Object doesn't support this property or method". The error is occuring...
2
by: jerry.ranch | last post by:
I'm starting to learn about the tab control. How would I write an on open event procedure in VBA, that upon opening of the form, a specific tab opens (say tab 1)? Thanks Jerry
8
by: Seth Darr | last post by:
I'm working on migrating an Classic ASP/VB6 COM application from an NT Server with IIS<6 to an virtual machine running Windows Server 2003 and IIS 6. I've worked through most of the obvious...
1
by: kbarrios | last post by:
Hi, I am working with VBScript and I put a "window.open" inside a "form action post" due that I am handing a "checkbox" on it, but the "window.open" doesn't work: <FORM...
3
by: Ron | last post by:
How to handle this when using custom membership provider. Please refer to my first question which is posted. "Custom Membership Provider" Thanks, Ron
4
by: R Reyes | last post by:
Whenever I right-click on "Open in New Tab/Window" my website ALWAYS opens in the same window/tab. This is most likely a programming issue because when I visit other websites it works just fine. ...
4
by: Takeadoe | last post by:
Dear NG - I've got a bunch of SQL statements (Insert Into "Access Table Name") generated from another software program. They look like this: Pasting these lines into the SQL view as they are...
14
by: W2K3R2admin | last post by:
I cannot open help and support...how do I install it on an existing system? When I click on Help & Support, the error says "Windows cannot open Help and Support because a system service is not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.