Got most of it done, but need some help...

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;
}
 
F

foobish

First off, since you are not doing anything with ofstream& out in
processBlank, you don't need to pass in to it.

I would increment the count when the character is a space instead of
when it is not a space. With this code, you are just counting the
number of characters that are not spaces. However, just counting the
spaces would still not give you an accurate word count either since
some people add two spaces after a sentence, hyphenated words, etc. It
would probably be sufficient for an introductory C++ school project
though.

Another issue you have is that you have nested loops for the input
stream. One in the main function (while (!inData.eof()) ) and another
inside processBlank. You probably need to get rid of the latter and
just use the one main.

Also, wordsLine is never set to anything besides 0, so when updateCount
is called paraTotal always gets incremented.

Jason
 
M

mlimber

Jintty said:
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);

Since your program is very linear in nature, dividing it up into
functions with many parameters like this seems superfluous, but perhaps
its a requirement of your assignment.
int main()
{
int wordsLine = 0;
int wordsTotal;
int linesTotal;
int paraTotal;
char letter;

Don't declare variables until you need them, and then declare them in
the smallest possible scope. It makes code easier to think about and
debug.
ifstream inData;
ofstream outData;

inData.open("inData.txt");
outData.open("outData.txt");

Prefer to open these via the constructor:

ifstream inData("inData.txt");
ofstream outData("outData.txt");
initialize(wordsLine, wordsTotal, linesTotal, paraTotal);
inData.get(letter);
while (!inData.eof())

Not reliable! See the FAQ:

http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5
{
outData << inData.rdbuf();

There are better ways to do what you're trying to do here. Avoid using
streambuf objects unless you must do so.
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();

Let the destructors do this automatically unless you have a compelling
reason to close the files early.
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;

The "return" is unnecessary here. It can do nothing but return at that
point. No need to clutter the code.
}


void processBlank(ifstream& in, ofstream& out, char& letter, int&
wordsTotal)
{
while (!in.eof())
in.get(letter);
if (letter != ' ')
++wordsTotal;

Uhh, your indenting suggests curly braces are missing here.
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 think there are likely other problems, but that should give you a
heads up. The first thing you need to do is to learn to use your
debugger. One probably came with your compiler or development
environment. It will allow you to step through the program line-by-line
and see what is happening (e.g., what character you just read in).

Cheers! --M
 
J

John Harrison

Jintty said:
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.

Strange problem, two completely unrelated tasks put into one program.
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

Well your logic is completely wrong.

"mary had a little lamb"

There are 18 non-blanks in that string but only 5 words.

Before you go any further you need formulate rules that actually work,
for what is a word, what is a line, and what is a paragraph. You
*cannot* solve the problem without doing that *first*.
please help me, i'd appreciate any help
thank you

I'm sorry but the rest of the code isn't 'got most of it done' it's an
illogical mish-mash that does nothing related to the problem at all. You
really should throw the code away and start again.

For instance to count that words, you could count the letters that are
at the beginning or a work. So in

"mary had a little lamb"

you count the m, h, a, l, and l. A rule for that would be, a character
is the beginning letter of a word if it's not a blank, and the previous
letter was a blank or if its not a blank and it's also the first
character in the file.

So you code will look like this

read a char
while (not end of file)
{
write char to output file;
if (rule for a beginning letter is true)
++wordCount;
read a char
}

It's a little more complex than the above, but not a lot.

john
 
I

int2str

John said:
Strange problem, two completely unrelated tasks put into one program.

I agree, but could imagine that the teacher had something in mind where
the bytes would be evaluated as they are being copied (state machine
anyone?). We'll never know without a clarification.

[snipped comments]
I'm sorry but the rest of the code isn't 'got most of it done' it's an
illogical mish-mash that does nothing related to the problem at all. You
really should throw the code away and start again.

I agree... There's too much logically wrong with the code that starting
from scratch seems sensible. Functions like "initialize" are a to me a
clear sign of over-zealousy and lack of grasp on the overall concepts.

Cheers,
Andre
 
J

John Harrison

I agree, but could imagine that the teacher had something in mind where
the bytes would be evaluated as they are being copied (state machine
anyone?). We'll never know without a clarification.

I wnoder if the original intent was to clean up the text as it was
copied, for instance remove multiple spaces, properly indent paragraphs etc.

To be fair to the OP I does seem too hard a task for a first program.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top