473,594 Members | 2,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2164
"Luis" <ki***@comcast. net> wrote in message
news:Jx1Pa.2220 4$ye4.17360@scc rnsc01...
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
2196
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 python-help, to point out a few resources that can help with answering your own questions, or improve the chances of getting a useful answer from the helpers. The most comprehensive overview of python.org help resources is at ...
4
14135
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 on the "With Me.OLEObject" line (By the way, I haven't found documentation which explains what the "With" clause is suppose to do?). I am trying to extract a Word document(OLE object) from an Access database, for each record in the table, and...
2
1942
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
2481
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 problems resulting from IIS 6 prohibiting ASP and SSI etc, but I've hit a peculiar wall. One ASP page uses a VB6 COM object that basically reads from an .INI file and uses it to open another file. The existing code uses this line (in VB6) to do this:...
1
5212
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 ACTION="baz_add_action.asp" METHOD="post"> ...here go the Window.open script <INPUT NAME="add<%=p_prod_id%>" TYPE="checkbox" value="ON">
3
1354
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
3541
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. Is this something that I can change with my code inside ASP.NET/C#.NET? And where? Does anyone know how to avoid this? It is very annoying and the website is not working as it should. People should be able to open new tabs/windows on my...
4
2513
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 generates a couple of different errors (missing semicolon being one of them). They work fine if I paste 1 line at a time. Can someone tell me what I need to do to remedy the situation? I can't hardly paste 1 at a time! Your help is very much...
14
58987
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 running. To fix this problem start the service named "help and support." I have tried cmd prompt and 'msconfig' I have double checked by going to control panel/administrative tools/services Help and Support is not listed in either place so I...
0
7941
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7874
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8231
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5738
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1476
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1205
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.