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.