• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

input files c++

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

1 Solution
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).

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


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.


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

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now