473,326 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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

Nov 22 '05 #1
5 1732
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

Nov 22 '05 #2
Jintty wrote:
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-lit....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

Nov 22 '05 #3
Jintty wrote:
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
Nov 22 '05 #4

John Harrison wrote:
Jintty wrote:
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 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

Nov 22 '05 #5
in*****@gmail.com wrote:
John Harrison wrote:
Jintty wrote:
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 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
Nov 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ixnay | last post by:
Hi All, The CFO of our company just asked me to write an app on top of an Access database. I've never written anything in VB and could use some help in getting started. I would, in particular,...
9
by: Steven T. Hatton | last post by:
This was written for the gnu.g++.help list. It rather clearly spells out the most important feature of Java that I believe C++ lacks. I really don't believe the C++ Standard sepcifies enough for a...
4
by: sparks | last post by:
We have a database that reads in and formats raw data. We were using queries to format the data per person and outputing reports. The other database has the persons personal information. I changed...
6
by: Peter Row | last post by:
Hi, I am writing a DLL in VB.NET that implements IHttpHandler.ProcessRequest. This code calls a sub and I need to know if that sub did a response redirect or not. Specifically I need to know...
5
by: Jonathan Smith | last post by:
I am thinking about attempting a program to help my colleagues at school to write their pupil reports. Each report has two sections: a list of skills with a seies of tick boxes to show the level...
5
by: darnnews | last post by:
Hi, I have been creating a database to keep track of press clippings, but I have hit a couple stumbling blocks. Any help is much appreciate. 1) Seeing if my query is done I have the...
5
by: JF Fortier | last post by:
Hi group I'm not familiar with the name, newbies user. The SQL key is call Row # ?? if that correct can it be possible to create a new record and replace that unique ID key to a previous erase...
4
by: =?ISO-8859-1?Q?Jo=E3o_Gomes?= | last post by:
Hi, i need to code in c something like this, www.searchbay.org?idapp=93896 can any one pointing me some tricks.. tks for your help.
1
by: pbd22 | last post by:
Hi. I have been asked to do a rather odd thing by my employer. I have to add a new feature to an old project that "should not know about the change". This is a bit of an odd request but just assume...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.