?
Solved

input files c++

Posted on 2003-11-14
6
Medium Priority
?
253 Views
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 ' ':
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

0
Comment
Question by:aj_blackmond
5 Comments
 
LVL 11

Accepted Solution

by:
bcladd earned 540 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).

-bcl
0
 
LVL 10

Expert Comment

by:Sys_Prog
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


HTH

Amit
0
 
LVL 1

Expert Comment

by:meow00
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") ;

  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
 
LVL 11

Expert Comment

by:bcladd
ID: 9755103
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
 
LVL 9

Expert Comment

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

Tinchos
EE Cleanup Volunteer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

749 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