detect repeated words example

Hello wonderful people of the internet!

here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13

#include "../../std_lib_facilities.h"
int main () {
string previous = " ";
	string current;
	while (cin >> current) {
		if (previous == current) {
			cout << "repeated word: " << current << '\n';
			previous = current;
		}
	}
return 0;
}


(this is an example in the Bjarne book). I've read over the explanation multiple times, but I still cannot understand.

What does string previous = " "; do?

It initializes previous to the character space (like when you press space). But I thought in CPP it doesn't read it, something about the compiler skipping over whitespace. Why initialize it to that then?

Another thing, I didn't tell the program to ask me for an input. I realize I have

while (cin >> current)

but I didn't have a line before saying something like "Please enter input" then next line:

cin >> input; or cin >> current

Without having the above, it still expects an input, but why is this?

Finally, in the book it says do try this: Use the input "The cat cat jumped". Ok, I get it the program should probably return to me cat twice, but it doesn't do that. Even the next 'try this' is telling me to input

She she laughed He He...etc he did did not.. etc.

I realize it wants me to learn that 'She' and 'she' are not the same. But when I enter these inputs, the program doesn't do anything. It only remains in an ongoing state of expecting an input. What's the deal with that?

Thank you very much for your help

-ilovejapanesegirls
Last edited on
I bet you have a typo. Look at lines 9 and 10.
Hello.

I looked at it multiple times, the only difference is that in the textbook, previous = current; is one tab to the left (and other indentation differences). But other than that I cannot see any difference from what I wrote and what was in the textbook.
What does string previous = " "; do?

It initializes previous to the character space (like when you press space). But I thought in CPP it doesn't read it, something about the compiler skipping over whitespace. Why initialize it to that then?


Precisely because formatted input extraction skips whitespace. You need previous to be something that current cannot be the first time through the loop, else you may have a false positive on line 7.

Of course, leaving it as an empty string would've been just as effective.
Hey guys I'm really sorry, I did in fact make a mistake. I had no idea you could omit {} with if statements (which is what the book did, and what I didn't do).

My last question would still be this: What's the deal with cin? This part is what I meant:
/*
Another thing, I didn't tell the program to ask me for an input. I realize I have
while (cin >> current)
but I didn't have a line before saying something like "Please enter input" then next line:
cin >> input; or cin >> current
Without having the above, it still expects an input, but why is this?
*/

Thank you guys for the patience.
Topic archived. No new replies allowed.