Please help with a looping program


1. Open and read from a file called ‘teach.txt’. Test that it opened correctly. Read till the end of the file.
2. As you are reading, output what you read into a second file called ‘ordered_teach.txt’.
3. ‘ordered_teach.txt’ should have 12 words to a line and an endl or ‘\n’ at the end
of each line (12 should be a const.). Each word should be followed by a space.
4. As you read, count the number of words and the number of lines you are outputting to the outfile. You may end with a partial line (less than 10 words): be sure to count that as a line and count the words in it.
5. At the end of the output file, print two endl’s, then the following lines:
“total number of words: “ followed by how many words are in the output file, followed by endl; “total number of lines: “ followed by how many lines you
printed.
6. Extra credit – 3 pts: As you are looping through the file, change each instance of the word “the” to “ZZZ”. Also, count the number of “the”s in the file.

Input File: type this exactly, or take it off Blackboard. Call it teach.txt

You who are on the road Must have a code that you can live by And so become yourself because the past is just a good bye. Teach your children well, Their father's hell did slowly go by, And feed them on your dreams The one they picked, the one you'll know by. Don't you ever ask them why, if they told you, you would cry, So just look at them and sigh and know they love you. And you, of tender years, Can't know the fears that your elders grew by, And so please help them with your youth, They seek the truth before they can die. Teach your parents well, Their children's hell will slowly go by, And feed them on your dreams The one they picked, the one you'll know by.

Sample output (with extra credit part)

You who are on ZZZ road Must have a code that you
can live by And so become yourself Because ZZZ past is just
a good bye. Teach your children well, Their father's hell did slowly
go by, And feed them on your dreams The one they picked,
ZZZ one you'll know by. Don't you ever ask them why, if
they told you, you would cry, So just look at them and
sigh and know they love you. And you, of tender years, Can't
know ZZZ fears that your elders grew by, And so please help
them with your youth, They seek ZZZ truth before they can die.
Teach your parents well, Their children's hell will slowly go by, And
feed them on your dreams The one they picked, ZZZ one you'll
know by.

Total number of words: 134
Total number of lines: 12
Total number of the's: 6




Here's what I've come up with thus far .. I've managed to do what she's asked .. but I can't seem to set 12 words per line .. or declare how many words there are/lines/the's ..

Please Help!!!


#include <iostream>
#include <fstream>
#include <string>
#include<iomanip>
using namespace std;

int main()
{
ifstream read;
ofstream write;
char readFile[] = "teach.txt";
char writeFile[] = "ordered_teach.txt";
int wordcounter, numofwords, the;
string words, overallwords;
const int linedwords = 12;

read.open(readFile);
write.open(writeFile);

if (!read || !write)
{
cout << "Unable to open file ";
if (!read)
{
cout << readFile << endl;
}
if (!write)
{
cout << writeFile << endl;
}
exit(1);
}
else
{
while (read >> words)
{
if (words == "the")
{
the++;
words = "ZZZ";
}
if (words >= overallwords)
{
wordcounter++;
cout << words << " ";
write << words << " ";
}
if (wordcounter % linedwords == 0)
{
write << endl;
numofwords++;
}
}
if (wordcounter % linedwords!=1);
{
wordcounter++;
write << endl;
cout << endl;
}

}

write << endl;
write << endl;
write << "Total number of words: " << wordcounter << endl;
write << "Total number of lines: " << numofwords << endl;
write << "Total number of the's: " << the << endl;

return 0;

}
Please use code tags to format your code ("<>" button on the right).

Well, first of all, you don't initialize any of your counters (wordcounter, numofwords, the) with 0, so they will contain garbage.

This: if (words >= overallwords) is a very weird line. What are you trying to achieve with this? What is overallwords? It is empty throughout the program.

Here: if (wordcounter % linedwords!=1); you need to remove the semicolon ; - it makes an empty block and everything below will be executed in any case.

Last edited on
Topic archived. No new replies allowed.