#include <fstream>
#include <iostream>
#include <cctype>
#include <cstdlib>
usingnamespace std;
//returns the sum of letters encountered in the file
long count_letters(ifstream& in_stream);
//returns the sum of words encountered in the file
long count_words(ifstream& in_stream);
int main()
{
ifstream in_stream;
in_stream.open("test3.txt");
if(in_stream.fail())
{
cout << "Input stream failed to open.\n";
cin.get();
exit(1);
}
long letters = count_letters(in_stream);
in_stream.close();
in_stream.open("test3.txt");
if(in_stream.fail())
{
perror("Input stream failed to open");
cin.get();
exit(1);
}
long words = count_words(in_stream);
cout << "The number of letters in the file is: " << letters << endl;
cout << "The number of words in the file is: " << words << endl << endl;
cout << "End of the program.";
char a;
cin >> a;
return 0;
}
long count_letters(ifstream& in_stream)
{
char next;
long i(0);
in_stream.get(next);
while(!in_stream.eof())
{
if (!isspace(next) && (next != ',') && (next != '.'))
{
i++;
}
in_stream.get(next);
}
return i;
}
long count_words(ifstream& in_stream)
{
char present;
do
{
in_stream.get(present);
}while ((isspace(present) || (present == '.') || (present == ',')) && (!in_stream.eof()));
if(in_stream.eof())
{
return 0;
}
char next;
long i(1);
in_stream.get(next);
while (!in_stream.eof());
{
if((!isspace(present) && (present != '.') && (present != ',')) && (isspace(next) || (next == '.') || (next == ',')))
{
i++;
}
present = next;
in_stream.get(next);
}
return i;
}
My count_words function doesn't seem to be working and I don't know why. BTW, for the purpose of this program a break between words can be a comma, period or a whitespace, so any of the aforementioned characters acts as a border between two words. Even nonsensical jargon such as "23:#" is a word provided that it has the aforementioned buffer characters to the right and left of it.
On a separate note is it possible to start reading from the beginning of an input file a second time? As it stands right now I use lines 26/27 to start reading the input file from the beginning but I have a feeling that there's a better way to do this. Thank you for your input.
I just realized that line 78 shouldn't have a semicolon in it. I'm still getting the wrong number of words but I'm guessing that's most of my problem solved. I can probably figure out the rest from here.
#include <fstream>
#include <iostream>
#include <cctype>
#include <cstdlib>
usingnamespace std;
//returns the sum of letters encountered in the file
long count_letters(ifstream& in_stream);
//returns the sum of words encountered in the file
long count_words(ifstream& in_stream);
int main()
{
ifstream in_stream;
in_stream.open("test3.txt");
if(in_stream.fail())
{
cout << "Input stream failed to open.\n";
cin.get();
exit(1);
}
long letters = count_letters(in_stream);
in_stream.close();
in_stream.open("test3.txt");
if(in_stream.fail())
{
perror("Input stream failed to open");
cin.get();
exit(1);
}
long words = count_words(in_stream);
cout << "The number of letters in the file is: " << letters << endl;
cout << "The number of words in the file is: " << words << endl << endl;
cout << "End of the program.";
char a;
cin >> a;
return 0;
}
long count_letters(ifstream& in_stream)
{
char next;
long i(0);
in_stream.get(next);
while(!in_stream.eof())
{
if (!isspace(next) && (next != ',') && (next != '.'))
{
i++;
}
in_stream.get(next);
}
return i;
}
long count_words(ifstream& in_stream)
{
long i(0);
char present;
do
{
in_stream.get(present);
}while ((isspace(present) || (present == '.') || (present == ',')) && (!in_stream.eof()));
/*if(!isspace(present) && (present != '.') && (present != ','))
{
i++;
}*/
if(in_stream.eof())
{
/*if(!isspace(present) && (present != '.') && (present != ','))
{
i++;
}*/
return i;
}
char next;
in_stream.get(next);
while (!in_stream.eof())
{
if((!isspace(present) && (present != '.') && (present != ',')) && (isspace(next) || (next == '.') || (next == ',')))
{
i++;
}
present = next;
in_stream.get(next);
}
return i;
}
I do have one question though. For most cases my program counts the number of words by counting the number of times that a letter is followed by a whitespace/period/comma. The if statements on lines 73 and 80 are meant to take into account the possibility of a file containing just one word with no spaces at the end i.e. a word with an end of file at the end. In such a case either of the if statements on line 73 or 80 should do the job but only the if statement on line 73 works correctly. Why is this?
It looks as though lines 73-76 will give a 1 and lines 78-85 will give a 0 regardless of the number of words in the file. The control expression in 69 will be false, stopping the loop, at the first instance there's a character other than a space and period and comma. It wouldn't necessarily be the end of the file.
Suppose the first two characters of the file are a ' ' and then a 't', with more characters following. When present equals 't' the loop stops, and the expression in 73 is true and the one in 78 is false.
You probably just don't need lines 66-85. And if you can assume the file has proper punctuation, such as "word, word" and "word. Word" and so forth with spaces never being on both sides of punctuation marks, you might need only to consider if next is a space in line 91. Perhaps initialize i to 1 so that for example from "word word word" you get i equals 3 and in the case of no spaces and one word in the file you get i equals 1.