Real Simple For You, Real Confusing For Me

Started by
13 comments, last by Mr McKnowNothing 20 years, 5 months ago
quote:Original post by Mr McKnowNothing
fredizzimo, I''m only seven chapters into a baby-C++ book. I haven''t done any work with classes yet. I would really appreciate it if you could dumb it down a bit.


Yes, I know, it was a bit technical, but I wasn''t answering your question. I was correcting groby, because, what he said was wrong. But you don''t need to worry about it, continue testing for eof like you currently do. Both methods will work however.

And I will answer your question now. Oldguy has the correct answer, and you don''t need any extra variables, to fix it.

In copyText the line
while ((ch != '' '') && ((ch != ''\n'') && (ch != ''\t'')))
needs to check for EOF so change it to
while ((ch != '' '') && (ch != ''\n'') && (ch != ''\t'') && in)

I removed a redundant parentesis, actually the other parentesis are redundant too, execpt the outermost, but it can look a bit cleaner this way. processBlank needs to be changed in a similar way.
Advertisement
Thanks for the advice, fredizzimo,
I fixed the program relative to your comments. Now it works perfectly (though not exactly the way the book wanted it to).
For my fellow ultra-noobs out there, here's a simple program that copies text from one file to another, counting the words, lines, and paragraphs (where paragraph splits are marked by a line with no words on it).

#include<fstream>#include<iostream>using namespace std;void initialize(int&y, int& z, int&w);void processBlank(ifstream& in, ofstream& out, char& ch, int& linesTotal, int& paraTotal);void copyText(ifstream& in, ofstream& out, char& ch, int& w);void printTotal(int& wordsTotal, int& linesTotal, int& paraTotal, ofstream& y);int main(){	int wordsTotal;	int linesTotal;	int paraTotal;	char letter;	ifstream inData;	ofstream outData;	inData.open("E:inData.txt");	outData.open("E:outData.dat");	initialize(wordsTotal, linesTotal, paraTotal);		inData.get(letter);	while (!inData.eof())	{		processBlank(inData, outData, letter, linesTotal, paraTotal);		copyText(inData, outData, letter, wordsTotal);	}	printTotal(wordsTotal, linesTotal, paraTotal, outData);	inData.close();	outData.close();	cout << endl;	return 0;}//////////////////////////////////////////////////////////////////////////part of the assignment/////////////////////////////////////////void initialize(int&y, int& z, int& w){	y = 0;	z = 0;	w = 0;	return;}/////////////////////////////////////////////////////////////////////////////processBlanks is used to cout the end of lines and new paragraphs,//new paragraphs are indicated as a line with nothing on it.///////////////////////////////////////////////////////////////////////////void processBlank(ifstream& in, ofstream& out, char& ch, int& linesTotal, int& paraTotal){	char peeker;		while ((ch == ' ') || (ch == '\t') || (ch == '\n'))	{		peeker = in.peek();			if((ch == '\n') && (peeker == '\n'))			++paraTotal;	if (ch == '\n')			++linesTotal;		out << ch;		in.get(ch);	}		return;}////////////////////////////////////////////////////////////////////////////////copyText counts words////////////////////////////////////////////////////////////////////void copyText(ifstream& in, ofstream& out, char& ch, int& w){	if ((ch != ' ') && (ch != '\n') && (ch != '\t'))		++w;	while ((ch != ' ') && (ch != '\n') && (ch != '\t') && in)	{		out << ch;		in.get(ch);	}		return;}/////////////////////////////////////////////////////////////printTotal reports the results of the previous functions///////////////////////////////////////////////////////////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;}


[edited by - Mr McKnowNothing on November 8, 2003 7:17:18 PM]

[edited by - Mr McKnowNothing on November 8, 2003 7:18:31 PM]
The program looks a bit more complicated then it needs to be
What you can do is just read your input file in char by char and send it to your output file. To decided on whether you should increase the counter check the last char you read in if it''s a space, tab, newline etc. Keep doing till input eof and just appended the counted info to the end of the output file.

So the code would look something like this:

char letter;//if it''s not eof() you know there''s got to be at least//one more char to processwhile(!inData.eof()){    //do char by char copy from one file to other    inData.get(letter);    outData<<letter;        //check letter and increment counters as needed    switch(letter)    {        case '' '':            wordTotal++;            break;        case ''\n'':            lineTotal++;            break;        case ''\t'':            paraTotal++;    }//switch}//whileprintTotal(wordTotal, lineTotal, paraTotal, outData);





--{You fight like a dairy farmer!}

--{You fight like a dairy farmer!}

Greatwolf,
I had that feeling. Thanks for the advice, that is a much better way to do this.

This was the chapter on user-defined functions, so I think the author went a little overboard in the structure of the problem.

Fredizzimo -

Thanks for the info. I knew about the operator!() part, but missed the cast operator. It''s probably a result of the fact that I avoid C++ streams like the plague

(MrMcKnowNothing - that doesn''t imply they''re bad and you shouldn''t use them. It just means *I* cannot stand them and have my own streams. And yes, there are some good reasons for it - but nothing you''ll encounter any time soon. So don''t let me distract you.)

- Robert

This topic is closed to new replies.

Advertisement