Hi! I am trying to write the program described below. Below the program I have included the text from the file I call. I have written the program, but I don't know what I have done wrong. Can anyone enlighten me? Thanks

Write a program that will compute the words in a file, the letters of a file, and the average word length (average number of characters per word) for a file that contains some text. A word is defined to be any string of symbols that is preceded and followed by one of the following at each end: a blank, a comma, a period, the beginning of a line or the end of a line. Your program should also work with the stream cin as the imput stream, although the function will not be called with cin as an argument in this program.

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

void repeat_line(ifstream& in_stream);

int main() 
{ 
string word; 
string letters;
ifstream fin;
ifstream in_stream; 
int CountWord = 0;
int CountLetters = 0;
char next;

fin.open("gettysburg.txt"); 

while(fin >> word) 
    { 
        CountWord++; 
    } 

fin.close();
fin.open("gettysburg.txt");

in_stream.get(next);
while(! in_stream.eof( ))
{
    cout << next;
    in_stream.get(next);
    repeat_line(in_stream, next)
}
if(!fin.eof( ))
   cout << "Not Done yet.";
else
    cout << "End of the file.";


cout <<"Total Words: "<< CountWord << endl;
cout <<"Total Letters: "<< CountLetters << endl;
cout <<"Average Characters per word: "<< (CountLetters/CountWord) <<endl;
      system("PAUSE");
      return 0;

} 
void repeat_line(ifstream& in_stream)
{
   char symbol;
   do
   {
      if(in_stream.eof())
        return;
      in_stream.get(symbol);

   } while(symbol!='\n');
}

GETTYSBURG:

Four score and seven years ago our fathers brought forth, 
upon this continent, a new nation, conceived in Liberty, 
and dedicated to the proposition that all men are created
equal. Now we are engaged in a great civil war, testing
whether that nation, or any nation so conceived, and so
dedicated, can long endure. We are met here on a great
battlefield of that war. We have come to dedicate a
portion of it as a final resting place for those who here
gave their lives that that nation might live. It is
altogether fitting and proper that we should do this. But
in a larger sense we can not dedicate -- we can not
consecrate -- we can not hallow this ground. The brave men,
living and dead, who struggled, here, have consecrated it
far above our poor power to add or detract. The world will
little note, nor long remember, what we say here, but can
never forget what they did here. It is for us, the living,
rather to be dedicated here to the unfinished work which
they have, thus far, so nobly carried on. It is rather for
us to be here dedicated to the great task remaining before
us -- that from these honored dead we take increased
devotion to that cause for which they here gave the last
full measure of devotion -- that we here highly resolve
that these dead shall not have died in vain; that this
nation shall have a new birth of freedom; and that this
government of the people, by the people, for the people,
shall not perish from the earth.

This is a problem with several parts. What you want to do is work on each part separately. First, work out a way to read words:

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

string get_word ( ifstream& in );

int main()
{
  ifstream in ( "input.txt" );

  if ( !in ) {
    cerr<<"Error opening input file"<<endl;
    return EXIT_FAILURE;
  }

  for ( ; ; ) {
    string s = get_word ( in );

    if ( in.eof() && s.empty() )
      break;

    if ( !s.empty() )
      cout<< s <<endl;
  }

  return EXIT_SUCCESS;
}

string get_word ( ifstream& in )
{
  char ch;
  string s;

  while ( in.get ( ch ) ) {
    if ( ch == ' ' || ch == ',' || ch == '.' || ch == '\n' )
      break;

    s.push_back ( ch );
  }

  return s;
}

Okay, it's ugly but it works. From there we can calculate the number of words and number of letters in the file since we know that the words we read are what we want:

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

string get_word ( ifstream& in );

int main()
{
  ifstream in ( "input.txt" );

  if ( !in ) {
    cerr<<"Error opening input file"<<endl;
    return EXIT_FAILURE;
  }

  unsigned int words ( 0 );
  unsigned int letters ( 0 );

  for ( ; ; ) {
    string s = get_word ( in );

    if ( in.eof() && s.empty() )
      break;

    if ( !s.empty() ) {
      ++words;
      letters += s.length();
    }
  }

  cout<<"The number of words was: "<< words <<endl;
  cout<<"The number of letters was: "<< letters <<endl;

  return EXIT_SUCCESS;
}

string get_word ( ifstream& in )
{
  char ch;
  string s;

  while ( in.get ( ch ) ) {
    if ( ch == ' ' || ch == ',' || ch == '.' || ch == '\n' )
      break;

    s.push_back ( ch );
  }

  return s;
}

Finding the average word length is trivial now that we have the total characters and words:

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

string get_word ( ifstream& in );

int main()
{
  ifstream in ( "input.txt" );

  if ( !in ) {
    cerr<<"Error opening input file"<<endl;
    return EXIT_FAILURE;
  }

  unsigned int words ( 0 );
  unsigned int letters ( 0 );

  for ( ; ; ) {
    string s = get_word ( in );

    if ( in.eof() && s.empty() )
      break;

    if ( !s.empty() ) {
      ++words;
      letters += s.length();
    }
  }

  cout<<"The number of words was: "<< words <<endl;
  cout<<"The number of letters was: "<< letters <<endl;
  cout<<"The average word length was: "<< letters / words <<endl;

  return EXIT_SUCCESS;
}

string get_word ( ifstream& in )
{
  char ch;
  string s;

  while ( in.get ( ch ) ) {
    if ( ch == ' ' || ch == ',' || ch == '.' || ch == '\n' )
      break;

    s.push_back ( ch );
  }

  return s;
}

Now that everything works, the final step is to make it into a function that can be called with cin as well as a file stream. This is no biggie since everything works already, it's just a few simple changes:

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

string get_word ( istream& in );
void file_stats ( istream& in );

int main ( int argc, char *argv[] )
{
  if ( argc > 1 ) {
    ifstream in ( argv[1] );

    if ( !in ) {
      cerr<<"Error opening input file"<<endl;
      return EXIT_FAILURE;
    }

    file_stats ( in );
  }
  else
    file_stats ( cin );

  return EXIT_SUCCESS;
}

string get_word ( istream& in )
{
  char ch;
  string s;

  while ( in.get ( ch ) ) {
    if ( ch == ' ' || ch == ',' || ch == '.' || ch == '\n' )
      break;

    s.push_back ( ch );
  }

  return s;
}

void file_stats ( istream& in )
{
  unsigned int words ( 0 );
  unsigned int letters ( 0 );

  for ( ; ; ) {
    string s = get_word ( in );

    if ( in.eof() && s.empty() )
      break;

    if ( !s.empty() ) {
      ++words;
      letters += s.length();
    }
  }

  cout<<"The number of words was: "<< words <<endl;
  cout<<"The number of letters was: "<< letters <<endl;
  cout<<"The average word length was: "<< letters / words <<endl;
}

Whenever you write a program, it should go like that. Separate the task into manageable chunks and solve the problem incrementally. It's much easier that way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.