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 ' ':
blank_count++;
break;
case '\n':
case '\t':
break;
case '.':
sentence_count++;
break;
default:
char_count++;
break;
}
}

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

aj_blackmondAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bcladdCommented:
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).

-bcl
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sys_ProgCommented:
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


HTH

Amit
0
meow00Commented:
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") ;

  while(!input.eof())
  {
    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.

meow.

0
bcladdCommented:
meow00:
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
    }

-bcl
0
tinchosCommented:
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.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

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.