Solved

input files c++

Posted on 2003-11-14
6
232 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
6 Comments
 
LVL 11

Accepted Solution

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

-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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

758 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

21 Experts available now in Live!

Get 1:1 Help Now