Hey there everyone! I found this site because I was looking for some help with my c++ course. I am trying to figure out how to make a program that can read in data from a .txt and then replace every 7th word with an underscore. It must print out the text twice; once without the blanks and once with the blanks.

I know what I need to do essentially, but I just don't know how to do it. I can get it to print the text from the file but I can't get it to count the words correctly (thus not being able to replace any words). The problem I am having is that I cannot assume that there is a space in front of a word (ie: "This sentence has 11 words,but it will only count 10.")


Also on the first text print it adds another period (or last char). How do I get it to not repeat that?
I have:

while(inFile)
	{	inFile.get(ch);
		cout << ch;
	}

the code you put in post 18 definitely works but I don't think my professor has taught us anything about "aword.length()" or "ispunct(ch)" "aword.erase()" and the "+=" operator so I don't know if I am allowed to use them. Though she did say we could use isalpha(ch) so I assume we can use similar ones.

also I don't need a total word count persay but I was going to use it to use the % 7 so I can replace the right words.. so I wouldn't need the number++ right? just the count int?

read text from a file. I have:

do
	{
		cout << "Please enter a filename: ";
		cin >> fileName ;
       
		inFile.open(fileName.c_str());
		inFile.clear();
		inFile.seekg(0L, ios::beg);
	}
	while(!inFile);

echo print text normally. (this is in my first post)

echo print text again but this time every 7th word is replaced by an underscore. (Its supposed to make a fill in the blank type of thing) This is where i need help.

So something like this?

Input file: input.txt

The quick black fox jumps over the lazy dog.

yields this output to the screen?

The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over ___ lazy dog.

yeah exactly like that actually.

also I can't understand why but my coding repeats the last char.

The quick brown fox jumps over the lazy dog..

yeah exactly like that actually.

also I can't understand why but my coding repeats the last char.

The quick brown fox jumps over the lazy dog..

I think you may be going through the loop one too many times here:

while(inFile)
	{	inFile.get(ch);
		cout << ch;
	}

You reach your last character. You read it in to variable ch. You display variable ch. That's the first period. You've not hit the end of file yet, so you go back up top. Now you attempt to execute this line:

inFile.get(ch);

There is nothing more to get, so that command fails. Variable ch remains unchanged. You display it again with the next line:

cout << ch;

Hence you display the last character twice. There are a variety of solutions to this. You could perhaps check the failbit before displaying ch. Or call inFile.good () before displaying. Also see the example in this links:
http://www.cplusplus.com/reference/iostream/istream/get.html
http://www.cplusplus.com/reference/iostream/ios/good.html

There are also other ways to change the loop so you don't get the duplicate.

Oh ok so I fixed that with this:

inFile.get(ch);
	while(inFile)
	{	
		cout << ch;
		inFile.get(ch);
	}

it checks for a bad ch before entering the loop. Thanks for the links :D

but now for the bigger problem.. how do I make every 7th word an underscore?

psuedocode would be something like

while (infile.get(ch))
if (it's a word) ----- a letter next to a space or punctuation
then count++
if (count % 7)
then replace with' __'
else
display word

Well, regardless of replacing the letters in every seventh word, I think you have problems you need to solve before that. Namely, you have to display each word twice (ignore the task of replacing every seventh word for now. Let's say you're displaying all of them for now). Thus you need to store all of the words.

The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.

Right now you are displaying one character at a time and not saving it. However, you need to save it since at the end of the file, you need to display

The quick brown fox jumps over the lazy dog.

All you have is one character worth of memory and it contains the last character read in, which is a period. How are you going to display words that are not stored anywhere?

I think you need to use strings and you'll need either an array of them or a vector of them in order to store all the words. You'll read data into this vector/array of strings. This all has to happen before worrying about how to handle every seventh word. Have you worked with strings and arrays/vectors before?

I have not heard of vectors or arrays before. I don't think that would be the proper solution at this point. There has to be a simpler way.

is there a way to test if the next or previous char was a certain char? ie. if you have a a letter (isalpha(ch)) and the next one is a space or (ispunct(ch)) then that would determine it was a word.

if you look at this post its the same thing that i need essentially http://www.daniweb.com/forums/thread108397-2.html but we haven't gone over string functions like erase() and length() so I don't think that would work either.

and i dont know what += is either. And I was planning on reading the file twice.

and i dont know what += is either. And I was planning on reading the file twice.

+= is an operator that increases the variable on the left side of += by an amount equal to the right side of the +=. So

int a = 5;
a+=4;

is equivalent to

int a = 5;
a = a + 4;

Both end up with 9 being stored in a. Where did the += come from? Are you looking at some code?

http://www.daniweb.com/forums/thread108397-2.html post 18:

his solution works, but some of the operators and string functions are not familiar to me and I am not sure if I am allowed to use them. My project description doesn't say I can't but I just like to be certain. Its a very similar solution to what I was thinking of.

http://www.daniweb.com/forums/thread108397-2.html post 18:

his solution works, but some of the operators and string functions are not familiar to me and I am not sure if I am allowed to use them. My project description doesn't say I can't but I just like to be certain. Its a very similar solution to what I was thinking of.

This line of his?

aword += ch;

If variable aword contained "hous" and variable ch contained 'e', the above command would add 'e' to the end of "hous" to get "house", and store it in the variable aword. So aword would now store "house" after that command.

Okay, you're not familiar with strings. How about C-style strings? Are you familiar with a declaration like this?

char* aWord;     // char* stands for C-String here

no this is my first computer class and its an intro to C++. I have used strings ( and I am encouraged to do so) just no mention of commands like str.erase() and str.length() has come up before. I guess if I knew a little more about them I would not feel so bad about using them. I just didn't want to rip off code, ya know?

I appreciate all the help you've given me. You explained everything very well for me.

This question has already been answered. Start a new discussion instead.