J
Jintty
Hi,
I'm trying to write a program that will read a txt file, copy it into
another text file and display the number of words, lines and
paragraphs.
I was able to get the copying portion done, but i'm struggling with the
counters.
processBlank is supposed to increment the number of words whenever it
hits a nonblank, so i figured using if its not equal to blank then
update word count but no luck
please help me, i'd appreciate any help
thank you
#include<fstream>
#include<iostream>
using namespace std;
void initialize(int& x, int&y, int& z, int&w);
void processBlank(ifstream& in, ofstream& out, char& ch, int&
wordsTotal);
void copyText(ifstream& in, ofstream& out, char& ch);
void updateCount(int& wordsLine, int& linesTotal, int& paraTotal, int&
wordsTotal);
void printTotal(int& wordsTotal, int& linesTotal, int& paraTotal,
ofstream& y);
int main()
{
int wordsLine = 0;
int wordsTotal;
int linesTotal;
int paraTotal;
char letter;
ifstream inData;
ofstream outData;
inData.open("inData.txt");
outData.open("outData.txt");
initialize(wordsLine, wordsTotal, linesTotal, paraTotal);
inData.get(letter);
while (!inData.eof())
{
outData << inData.rdbuf();
cout << "Running."<< endl;
processBlank(inData, outData, letter, wordsTotal);
cout << wordsTotal;
copyText(inData, outData, letter);
updateCount(wordsLine, linesTotal, paraTotal, wordsTotal);
inData.get(letter);
}
printTotal(wordsTotal, linesTotal, paraTotal, outData);
inData.close();
outData.close();
cout << endl;
cin.get();cin.get();
return 0;
}
void initialize(int& x, int&y, int& z, int& w)
{
x = 0;
y = 0;
z = 0;
w = 0;
return;
}
void processBlank(ifstream& in, ofstream& out, char& letter, int&
wordsTotal)
{
while (!in.eof())
in.get(letter);
if (letter != ' ')
++wordsTotal;
return;
}
void copyText(ifstream& in, ofstream& out, char& ch)
{
while ((ch != ' ') && (ch != '\n') && (ch != '\t') && in)
{
out.put(ch);
in.get(ch);
}
return;
}
void updateCount(int& wordsLine, int& linesTotal, int& paraTotal, int&
wordsTotal)
{
if(wordsLine == 0)
paraTotal++;
else
linesTotal++;
return;
}
void printTotal(int& wordsTotal, int& linesTotal, int& paraTotal,
ofstream& y)
{
y << endl << "Total number of words: " << wordsTotal << ".";
y << endl << "Total number of lines: " << linesTotal << ".";
y << endl << "Total number of paragraphs: " << paraTotal << ".";
return;
}
I'm trying to write a program that will read a txt file, copy it into
another text file and display the number of words, lines and
paragraphs.
I was able to get the copying portion done, but i'm struggling with the
counters.
processBlank is supposed to increment the number of words whenever it
hits a nonblank, so i figured using if its not equal to blank then
update word count but no luck
please help me, i'd appreciate any help
thank you
#include<fstream>
#include<iostream>
using namespace std;
void initialize(int& x, int&y, int& z, int&w);
void processBlank(ifstream& in, ofstream& out, char& ch, int&
wordsTotal);
void copyText(ifstream& in, ofstream& out, char& ch);
void updateCount(int& wordsLine, int& linesTotal, int& paraTotal, int&
wordsTotal);
void printTotal(int& wordsTotal, int& linesTotal, int& paraTotal,
ofstream& y);
int main()
{
int wordsLine = 0;
int wordsTotal;
int linesTotal;
int paraTotal;
char letter;
ifstream inData;
ofstream outData;
inData.open("inData.txt");
outData.open("outData.txt");
initialize(wordsLine, wordsTotal, linesTotal, paraTotal);
inData.get(letter);
while (!inData.eof())
{
outData << inData.rdbuf();
cout << "Running."<< endl;
processBlank(inData, outData, letter, wordsTotal);
cout << wordsTotal;
copyText(inData, outData, letter);
updateCount(wordsLine, linesTotal, paraTotal, wordsTotal);
inData.get(letter);
}
printTotal(wordsTotal, linesTotal, paraTotal, outData);
inData.close();
outData.close();
cout << endl;
cin.get();cin.get();
return 0;
}
void initialize(int& x, int&y, int& z, int& w)
{
x = 0;
y = 0;
z = 0;
w = 0;
return;
}
void processBlank(ifstream& in, ofstream& out, char& letter, int&
wordsTotal)
{
while (!in.eof())
in.get(letter);
if (letter != ' ')
++wordsTotal;
return;
}
void copyText(ifstream& in, ofstream& out, char& ch)
{
while ((ch != ' ') && (ch != '\n') && (ch != '\t') && in)
{
out.put(ch);
in.get(ch);
}
return;
}
void updateCount(int& wordsLine, int& linesTotal, int& paraTotal, int&
wordsTotal)
{
if(wordsLine == 0)
paraTotal++;
else
linesTotal++;
return;
}
void printTotal(int& wordsTotal, int& linesTotal, int& paraTotal,
ofstream& y)
{
y << endl << "Total number of words: " << wordsTotal << ".";
y << endl << "Total number of lines: " << linesTotal << ".";
y << endl << "Total number of paragraphs: " << paraTotal << ".";
return;
}