Member Avatar for cnet1989

I'm a computer science major and I'm taking an intro to C++ programming course. I have to write a program that reads in the data from a .txt file and echoprint it, and also print the data again but with every fifth word with 10 underscores. I was able to achieve the first half (echoprinting it) but how would I be able to reprint it replacing evey 5th word with 10 uderscores. It's supposed to be a cloze test...
Note: I'll be using a const string UNDERLINE = "__________"
please e-mail me back if you can at <snipped email>
I'll paste in the code that I currently have, but I'm not sure how it'll show up here.

#include <iostream> // Include Standard I/O Libraries
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
char cloze[1100];
char ch; 
const string UNDERLINE = "__________"
ifstream inFile;

cout << "Please enter a filename: " << endl;
cin >> cloze;
cout << "\n\n";
inFile >> ch;

inFile.open ( "cloze.txt" );
inFile.clear();
inFile.seekg(0L, ios::beg);

if ( !inFile )
{
cout << "Error opening file." << endl;
}
else
{ 
while ( inFile ) 
{ 
inFile.get(ch); 
cout << ch; 
}

} // end of else else
inFile.close();

return (0); 
} // end of main
ndeniche commented: naught naughty boy... posting twice the same thread is WRONG... +0

>>It's supposed to be a cloze test...
Doesn't that mean you're supposed to write the program without help from anyone? If yes, then isn't asking us for help the same as cheating by looking in your textbook?

line 19: why is the filename hardcoded instead of using the name you have to type at line 15?

delete lines 20 and 21 because that's the default behavior when a file is opened.

Member Avatar for cnet1989

no...its not an exam or anything....i have to make a program that functions like a cloze test.

what do you mean by "cloze <sinc> test" ? BTW: its spelled close not cloze. Do you mean to close the file and reopen it for the second reading?

Member Avatar for cnet1989

what do you mean by "cloze <sinc> test" ? BTW: its spelled close not cloze. Do you mean to close the file and reopen it for the second reading?

The proper spelling is cloze... a cloze test is like if you were given a paragraph with blanks in it. and you had to fill them in....thats the kind of program this is.

ok, you're right it is cloze -- my bad. I should have looked it up before critizing you for that.

Back to your problem: lines 23 thru 35 are all wrong. You want to read the file one word at a time, not one character at a time. The >> operator will do that very easily

std::string word;
while(inFile >> word)
{
    cout << word << " ";
}

now after the above finished just rewring the file using seekp() then clear all errors and read it the second time with the replacements you have to do.

Member Avatar for cnet1989

wow thank you. I have to go to work right now but I'll definately try that when I get back....is that hardcoded thing you talking about still ok or does that need to be changed? (the program has to read the cloze.txt file from the same directory the project is in. [ms visual studio express])

also is that what is called using an array??

If you are going to hardcode the filename then there is no point in lines 14, 15 and 16, so you might as well delete them.

>>also is that what is called using an array??
No. Nothing I posted uses an array. I see no reason to use an array in this program, unless there is some requirements that you didn't post.

Member Avatar for cnet1989

well i have requirements to follow but it doesn't say i can't use an array but I'm sure it has to be something we covered in class and i don't think std::string word is something we covered.

Member Avatar for cnet1989

If you are going to hardcode the filename then there is no point in lines 14, 15 and 16, so you might as well delete them.

>>also is that what is called using an array??
No. Nothing I posted uses an array. I see no reason to use an array in this program, unless there is some requirements that you didn't post.

My teacher requires that we ask user for input..

Well in that case you need to use the answer in the open statement instead of hardcoding the filename. line 19 should be this: inFile.open ( cloze );

well i have requirements to follow but it doesn't say i can't use an array but I'm sure it has to be something we covered in class and i don't think std::string word is something we covered.

If you have not covered std::string yet then replace it with a simple char array

char word[40];
while( inFile >> word )
{
   // blabla
}
Member Avatar for cnet1989

thank u for all ur help man!....or woman!

Member Avatar for cnet1989

Ok....I've been able to kind of count the words. What should I do to make sure when i check for white spaces (at the beginning of the file i.e.
" hello world") so that it doesn't produce a word after each space.

Member Avatar for cnet1989

I'm sorry to bring this back up.. I've accomplished counting the words but I have to be able to replace every fifth word from the opened file with "__________" ... I'm almost sure that this has to be done with a loop and a bool true/false thing...but How?? here's my code so far:

#include <iostream>                // Include Standard I/O Libraries
#include <fstream>
#include <string>
#include <cctype>

using namespace std;

int main()
{
	int number = 0,								// Set Counters to 0
			 i = 0,
			 wordMax = 5; 
	int numwords;
	
	bool lastWasLetter = true;
	char ch;
	const string UNDERSCORE = "__________";	    // Declare string for underscores
	string cloze;								// Declare string for input
	
	ifstream inFile;
	


 
	do 
		{
			// Prompt user for file name
			cout << "Please enter a file name: " << endl;
			cin >> cloze;
			

			// Open file and set read pointer to beginning of file
			inFile.open( cloze.c_str() );	// Open file
			inFile.clear();					// Move read pointer to beginning
			inFile.seekg(0L, ios::beg);
		}
		while ( !inFile );	// Check if file opens, recover from fail state
		

		
	while ( inFile )	    // While file is open
		{	
			inFile.get(ch);				    // Get characters from file
			cout << ch;					    // Echoprint the file
		}


		inFile.clear();					// Move read pointer to beginning
		inFile.seekg(0L, ios::beg);
		inFile.get(ch);					// Get Characters from text
		// Count the number of words in the file
		
	    	while ( inFile.get(ch) )
			{
				
				if ( ispunct(ch) )
					{	
						number++;
						lastWasLetter = true;
					}

				else if ( ( ch == ' ') )
						{
							number++;
							lastWasLetter = false;
						}

				else if ( ( ch == '\n' ) )
					{
						number--;
						lastWasLetter = false;
					}
			}
			 
			cout << "This file has " << number << " words." << endl;
			 
	/*	inFile.clear();					// Move read pointer to beginning
		inFile.seekg(0L, ios::beg);
		inFile.get(ch);	
			
				for(i = 0; isspace(ch), i < wordMax; i++)
		inFile.get(ch);
		cout << ch ;                       // I was trying to replace every 5th word but...idk */

	inFile.close();
	return 0 ; 
}

Maybe you get some ideas/clues from below, the important changes are that
the file is read in one word at a time + the use of operator %

>> i don't think std::string word is something we covered.

Hmm, you already have there a std::string, namely the 'cloze'.

...	
        string aWord;
	unsigned int WordCount = 0;

	// Read the file in word by word 
	// (a 'word' is here anything non-whitespace)

	while (inFile >> aWord)	    // While inFile succeeds in reading a word from the file
	{	
		// output the word
		cout << aWord;
		WordCount ++;
	}

	inFile.clear();			 // Move read pointer to beginning
	inFile.seekg(0L, ios::beg);

	unsigned int Count = 0;

	// re-read it again the same way as above
	while (inFile >> aWord)	    // While inFile succeeds in reading a word from the file
	{
		++ Count;

		// every 5th word is to be replaced, 
		// use the modulus operator (%) 
		if( ! (Count % 5))
		{
			 // replace it here

			// reset counter ...
			Count = 0;
		}
		else
		{
			// don't replace
		}    
	}

return 0 ; 
}
Member Avatar for cnet1989

mitrmkar, in the code u gave me, what's the purpose of WordCount++? And also, I need to reprint the file WITH the spaces (hence inFile.get(ch) ) AND with every 5th word replaced with 10 underscores. When I tried it this way It couned every 5 characters and the printed the underlines.

<< mitrmkar, in the code u gave me, what's the purpose of WordCount++?
WordCount counts the words in the file, the ++ operator increments the value
of WordCount by one, it could also be written as: WordCount = WordCount + 1.

<< And also, I need to reprint the file WITH the spaces (hence inFile.get(ch) ) AND with every 5th
<< word replaced with 10 underscores.
OK. I get it.

Then one solution is to read the file character by character like you already do. And using a std::string to accumulate the non-whitespace characters (i.e. other than '\n', ' ' and punctuation (there are actually more of those, but that's not important now))
So every time you hit a '\n', ' ' or punctuation, at that time, if you have any characters accumulated, that makes a word.

<snip>
string aword;
unsigned int count = 0;

while ( inFile.get(ch) )
{
        // todo: check for a tabulator too
	if ( ispunct(ch) || ( ch == ' ') || ( ch == '\n'))
	{	
		if(aword.length())
		{
			// we have one or more characters, so that
			// makes us a word ...
			++ number;
			++ count;

			if( ! (count % 5))
			{
				// every fifth word is to be filtered out
				cout << "_____";
                                // reset the counter
				count = 0;
			}
			else
			{
                                // output the current word
				cout << aword;
			}

			// reset the word to empty
			aword.erase();
		}

		// output the separating character to keep the output intact
		cout << ch;
	}
	else
	{
		// append the character to the word
		aword += ch;
	}
}
cout << "This file has " << number << " words." << endl;
Member Avatar for cnet1989

Thank You So Much For Your Help!!!

Member Avatar for cnet1989

May I ask that this thread be deleted please?

May I ask that this thread be deleted please?

Short answer. No. It's not our policy to delete threads on request, even of the OP. The point is that this thread might help others and also contains posts that other members have taken the time to write.

May I ask why you want this to be deleted? Especially after so long..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.