input files c++

Posted on 2003-11-14
Last Modified: 2010-04-01
i was able to try to write a function, however, i am still stuck. here are the directions and the program.
write a function that counts and displays on the screen the number of words in an input file. the program assumes that words in the file are separated by blank
spaces or end of lines. test the program with file containing no words, one word, and several words.

#include <iostream>
#include <fstream>
using namespace std;

int main()
int blank_count = 0;
int char_count = 0;
int sentence_count = 0;
char ch;

ifstream iFile("storm1.txt");

if (! iFile)
cout << "Error opening input file" << endl;
return -1;

while (iFile.get(ch))
switch (ch) {
case ' ':
case '\n':
case '\t':
case '.':

cout << "There are " << blank_count << " blanks" << endl;
cout << "There are " << char_count << " characters" << endl;
cout << "There are " << sentence_count << " sentences" << endl;

return 0;

here is my results when i run the program. WHAT AM I DOING WRONG!!! HELP!!!
There are 0 blanks
There are 0 characters
There are 0 sentences
Press any key to continue

Question by:aj_blackmond
LVL 11

Accepted Solution

bcladd earned 135 total points
ID: 9752663
What is the content of storm1.txt? I am guessing that it is empty (0 words).

The program works for me using Borland C++ 5.5 on WinXP Pro. YMMV.

You should indent your code to demonstrate the structure of the program.

Your program SHOULD (according to the assignment)  have another function in it, the function that does the real work. I would urge you to pass that function the name of a file. Then you can call it from main (in this case with "storm1.txt" if you wanted to count that file's contents).

Also note that you count the number of CHARACTERS, not the numer of WORDS as assigned. In case the person who gave you the code didn't tell you, one of the surest signs of academic dishonesty is an "answer" that fails to actually address the assignment (I have experience bringing honor council cases as well as sitting on the honor council). Just something to keep in mind. You want to make sure that you understand what this code does and then look at modifying it to count words. A suggestion: Use the >> operator (it is your firiend for this assignment).

LVL 10

Expert Comment

ID: 9752735
The above program would count the alphabet count rather than word count as required

This is because u have read it using char ch,

read it using a char * OR a char * OR a string type of object



Expert Comment

ID: 9754967
Hello aj_blackmond,

   Here is a possible code that will calculate the number of the words:
#include <iostream>
#include <fstream>
#include <string>

int main()
{ int count ;
  string temp ;
  ifstream input("Test.txt") ;

    input >> temp ;
    count ++ ;
  count -- ;

  cout << "There are " << count << " words in the file !" << endl ;
  return 0 ;
the Test.txt looks like the follows :

Hello! I am a cat. My name is Meow.
I don't like dogs. I like to catch fishes
and birds.
So the output becomes :
There are 20 words in the file !
  The line "count--" is because that "input.eof()" always count one more element at the end, but I am not quite sure why. Maybe some other experts could explain it better ?? Thanks.


LVL 11

Expert Comment

ID: 9755103
A couple of problems with your code:

    - You use count (in particular you increment it) without ever having initialized its value. C++ does not initialize memory used by built-in data types; you have no way of knowing what value count started with and therefore no reason to expect that count's final value has any relationship to the number of words in the file. Thus if you consistently came out 1 over you were very, very lucky.
    - Actually, your code is performing as if count begins with the value 0. The excess 1 comes from the fact that eof is not set until you try to read past the end of the file. That is, your program tries to read AFTER "birds." in your sample file. It fails to read anything, sets the eof flag, and then your next line increments count. Thus you counted a phantom word after "birds.".

So your code SHOULD initialize count to 0 (to make sure it has a known value). Also, the C++ idiom for reading a file by word is

    while (input >> temp) {
        // process  or count temp


Expert Comment

ID: 10249217
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: bcladd {http:#9752663}

Please leave any comments here within the next seven days.

EE Cleanup Volunteer

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now