This program is a word counter. But my logic is all messed up when it comes to the counting. I am not too sure on how about fixing it. The project compiles yet does not work properly. I still haven't figured yet how to count lines or characters properly.
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cctype>
#include <cstring>
using namespace std;
//opens the files
void openfiles(ifstream &fin, ofstream &fout);
//starts the count for text analysis
void startcount(int &countwd, int &countline, int &countch, int &countalpha,
int &countnum, int &countpunct, int &countother,
ifstream &fin, ofstream &fout);
//shows results for text analysis
void endtotal(int countwd, int countline, int countch, int countalpha,
int countnum, int countpunct, int countother, ofstream &fout);
//closes the files
void closefiles (ifstream &fin, ofstream &fout);
int main()
{
char text;
int countwd, countline, countch, countalpha;
int countnum, countpunct, countother;
ifstream fin;
ofstream fout;
openfiles(fin, fout);
startcount(countwd, countline, countch, countalpha,
countnum, countpunct, countother, fin, fout);
endtotal(countwd, countline, countch, countalpha,
countnum, countpunct, countother, fout);
closefiles(fin, fout);
return 0;
}
void openfiles(ifstream &fin, ofstream &fout) //opens files for program
{
fin.open("a:\\datain.txt", ios::in);
if(!fin.is_open())
{
cerr << "Unable to open input file, process terminated!\n";
exit(1);
}
fout.open("a:\\dataout.txt", ios::out);
if(!fout.is_open())
{
cerr << "Unable to open input file, process terminated!\n";
exit(1);
}
return;
}
void startcount(int &countwd, int &countline, int &countch, int & countalpha,
int &countnum, int &countpunct, int &countother,
ifstream &fin, ofstream &fout) //starts text analysis count
{
char character;
char text;
int i, j; //variable to help in looping
countwd = 1;
countch = 0;
cout << "Please enter text to be analyzed:\n"; //enters text for program
fin >> text; //sends information in for files
cin >> text;
for (i = 0; i < text; i++)
{
if (isspace(i))
{
countwd++;
}
}
for (j = 0; j < text; j++)
{
if (isalpha(j))
{
countch++;
}
else if (isdigit(j))
{
countch++;
}
else if (ispunct(j))
{
countch++;
}
}
if(isalpha(character))
{
countalpha++; // letter count
}
else if (isdigit(character))
{
countnum++; // number count
}
else if (ispunct(character))
{
countpunct++; // punctuation count
}
else
{
countother++; // space count
}
return;
}
void endtotal(int countwd, int countline, int countch, int countalpha,
int countnum, int countpunct, int countother, ofstream &fout)
{
if(countline > 0)
{
fout << "line count" << " " << countline << endl;
cout << "line count" << " " << countline << endl;
}
if(countwd > 0)
{
fout << "word count" << " " << countwd << endl;
cout << "word count" << " " << countwd << endl;
}
if(countch > 0)
{
fout << "char count" << " " << countch << endl;
cout << "char count" << " " << countch << endl;
}
if(countpunct > 0)
{
fout << " " << "punctuation" << " " << countpunct << endl;
cout << " " << "punctuation" << " " << countpunct << endl;
}
if(countnum >= 0)
{
fout << " " << "numeric" << " " << countnum << endl;
cout << " " << "numeric" << " " << countnum << endl;
}
if(countalpha > 0)
{
fout << " " << "alphabetic" << " " << countalpha << endl;
cout << " " << "alphabetic" << " " << countalpha << endl;
}
if(countother > 0)
{
fout << " " << "other" << " " << countother << endl;
cout << " " << "other" << " " << countother << endl;
}
return;
}
void closefiles (ifstream &fin, ofstream &fout)
{
fin.close();
fout.close();
return;
}