I understand the Repeated Word Detection program alot better but just one thing...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string current;
   string previous = "0";

   while (cin>>current){ // I enter "bacon bacon hello hello" (It only reads the 1st bacon and executes the rest of the code)
        if(previous == current) // 1st loop: 0 is not equal to bacon so it's false,              2nd loop: bacon is equal to bacon    3rd loop: bacon is equal to bacon 
            cout << "Repeated Word: " << current << "\n"; // 1st loop: Nothing will print,      2nd loop: Prints out "Repeated word: bacon"       3rd loop: Prints out "Repeated word: bacon" again
        previous = current; // 1st loop: 0 is set to bacon         2nd loop: bacon is set to bacon             3rd loop: bacon is set to bacon again and so on...
}
}



What I don't get is why doesn't the program keep printing out Repeated word: bacon over a thousand times since bacon is always going to be equal to bacon after the 2nd loop. This is what happens in my mind but in the program if I enter bacon bacon hello hello it prints out:
1
2
Repeated word: bacon
Repeated word: hello


I don't get this because i've been told that the ">>" operator ignores whitespace so how does the program get the "hello" part of my program if it only reads the 1st word after whitespace and then ignores everything else that I inputted, how does that work?

(I apologise for these comments on my code but this is the only way I can explain what should happen (from my perspective) to the program, thanks for understanding.
Last edited on
Let me clarify:

Take it as one word / input per loop.

First loop:
1. You're asked for input. (bacon bacon hello hello)
2. If statement compares previous with current, it returns false since it's not the same. (as of now, current = bacon, and previous is "0")
3. It falls down to the rest of the code, which assigns current (bacon) to previous, so previous is now bacon.

Second loop:
1. It looks at the next word, which is bacon
2. If statement returns true, and prints out "Repeated Words: bacon"
3. assigns current to previous, so bacon = bacon.

Third loop:
repeat the process of first loop, and do the same with any different word.

It makes sense to print out what only one bacon and hello only.

You could look at like this, a space you type (during program execution) is the same as if you entered "enter" after typing bacon. So, it's like this: bacon bacon hello hello, is the same as:

bacon
bacon
hello
hello

And as always, I'm just as beginner as you are. Any experienced programmer approve my post, else just ignore it. (Actually thinking of not posting until I have actual experience)
Ned757 wrote:
"I don't get this because i've been told that the ">>" operator ignores whitespace so how does the program get the "hello" part of my program if it only reads the 1st word after whitespace and then ignores everything else that I inputted, how does that work?"

reply:
From what I understand cin will keep reading the input stream one word at a time with each loop.
The problem I have with your code is the condition cin>>current returns true even when the input stream buffer is emtpy. Probably it is always true because it is waiting for the next input. There must be some way to flag the user isn't going to make anymore inputs.

Josephreak wrote:
"And as always, I'm just as beginner as you are. Any experienced programmer approve my post, else just ignore it. (Actually thinking of not posting until I have actual experience)"

reply:
The blind leading the blind :) Although I have done some C programming a long time ago and currently use BASIC I am also a beginner with c++. Trying to solve these beginner problems is part of the self learning even if you get it wrong.

@CodeWriter This code is actually from Bjarne Stroustrup (Creator of C++) in the book "Principles and Practice using C++". So you can't disagree with this code :D And so from what you told me, the first loop it will read bacon, then go to the 2nd word which is bacon, then 3rd word which is hello, then the 4th word which is hello? Please don't mix my brain up guys. I would appreciate a person who knew the answer 100%
Last edited on
Wait I think I figured it out. In the first loop it reads the 1st word which is bacon, in the second loop it reads the 2nd word which is bacon, in the 3rd loop it reads the 3rd word which is hello and in the 4th loop it reads the 4th word which is hello. So basically by saying ">>" operator ignores whitespace, it means it reads the first word inputted and executes the code, if their is a loop it then reads the 2nd word and so on. I am 95% sure I am correct because that definately makes sense now. Can some experienced program approve of this?
Last edited on
I think you will find I am right. I can figure it out from its behavior. Maybe different versions of c++ deal with it differently. Experts often don't realize that what is obvious to them isn't to a beginner.
You need to specifically test for no more words are to be waited for.
Below this is done by typing the word "exit"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string current = "";
   string previous = "";

   while (current != "exit"){
       cin >> current;
        if(previous == current)
            cout << "Repeated Word: " << current << "\n";
        previous = current;
   }
}

Last edited on
To properly understand the behavior would probably have to know about the class cin and its overloaded methods.
c++ is very complicated compared with a simple INPUT and PRINT found in BASIC.

If you are comparing numbers it will exit if you enter a string because it generates an error making the condition false ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;

int main()
{
   int current = 0;
   int previous = 0;

   while (cin >> current){
        cout << current << endl;
        if(previous == current)
            cout << "Repeated Word: " << current << "\n";
        else
            cout << "different word: " << current << "\n";
        previous = current;
   }
}

Last edited on
@CodeWriter, you can Ctrl+Z to terminate the loop but am I right about what I said:

Wait I think I figured it out. In the first loop it reads the 1st word which is bacon, in the second loop it reads the 2nd word which is bacon, in the 3rd loop it reads the 3rd word which is hello and in the 4th loop it reads the 4th word which is hello. So basically by saying ">>" operator ignores whitespace, it means it reads the first word inputted and executes the code, if their is a loop it then reads the 2nd word and so on. I am 95% sure I am correct because that definately makes sense now. Can some experienced program approve of this?
Well yes I knew that I didn't realize it was your issue I thought it was some issue around the logic of the code maybe from your previous thread. One or more white spaces separate the each word or number that is to be read however it leaves the terminating character in the stream which was my issue with the code example. Cntrl+ Z is an add hoc unfriendly solution which a user would have to know about. The getline does remove the terminating character.

Last edited on
So I am right about what I said in that paragraph, yes? Just to make sure.
Last edited on
I think so :)
If you run this code you can enter the four words separated by spaces and each cin will read and print the next word it will not read all the words with the first cin.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string current;
   string previous = "0";

   cin  >> current;
   cout << current << "\n";
   cin  >> current;
   cout << current << "\n";
   cin  >> current;
   cout << current << "\n";
   cin  >> current;
   cout << current << "\n";
}

Last edited on
Thank you :D
Topic archived. No new replies allowed.