Hey pps! I am learning C++ now and stuck on a exercise, can understand it, like at all :( help me pls!
So here os an example ://////////////////////////////////////////////////////////////////////////////////////////////////////
3.5.1 An example: detect repeated words
Assignment is needed when we want to put a new value into an object. When you think of it, it is obvious that assignment is most useful when you do things many times. We need an assignment when we want to do something again with a different value. Let’s have a look at a little program that detects adjacent repeated words in a sequence of words. Such code is part of most grammar checkers:
1 2 3 4 5 6 7 8 9 10
|
int main()
{
string previous = " "; // previous word; initialized to “not a word”
string current; // current word
while (cin>>current) { // read a stream of words
if (previous == current) // check if the word is the same as last
cout << "repeated word: " << current << '\n';
previous = current;
}
}
|
This program is not the most helpful since it doesn’t tell where the repeated word occurred in the text, but it’ll do for now. We will look at this program line by line starting with
string current; // current word
This is the string variable into which we immediately read the current (i.e., most recently read) word using
while (cin>>current)
This construct, called a while-statement, is interesting in its own right, and we’ll examine it further in §4.4.2.1. The while says that the statement after (cin>>current) is to be repeated as long as the input operation cin>>current succeeds, and cin>>current will succeed as long as there are characters to read on the standard input. Remember that for a string, >> reads whitespace-separated words. You terminate this loop by giving the program an end-of-input character (usually referred to as end of file). On a Windows machine, that’s Ctrl+Z (Control and Z pressed together) followed by an Enter (return). On a Unix or Linux machine that’s Ctrl+D (Control and D pressed together).
So, what we do is to read a word into current and then compare it to the previous word (stored in previous). If they are the same, we say so:
if (previous == current) // check if the word is the same as last
cout << "repeated word: " << current << '\n';
Then we have to get ready to do this again for the next word. We do that by copying the current word into previous:
previous = current;
This handles all cases provided that we can get started. What should this code do for the first word where we have no previous word to compare? This problem is dealt with by the definition of previous:
string previous = " "; // previous word; initialized to “not a word”
The " " contains only a single character (the space character, the one we get by hitting the space bar on our keyboard). The input operator >> skips whitespace, so we couldn’t possibly read that from input. Therefore, the first time through the while-statement, the test
if (previous == current)
fails (as we want it to).
//////////////////////////////////////////////////////////////////////////////////////////
So i tried to write this programm step by step, and here is what i get :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
int main()
{
string previous = " "; //define variable previous with type string and initializing variable previous with value " "
string current; //define string current with type string
while (cin >> current) { //inpit is "The cat cat jumped", current = "the"
if (previous == current) //
cout << "repeated word: " << current << '\n'; //skip
previous = current; //previous = "the"
while (cin >> current) { //inpit is "The cat cat jumped", current = "the"
if (previous == current) //yes
cout << "repeated word: " << current << '\n'; //output: "repeated word: The"
previous = current; //previous = "the"
//From now on = infinite loop.
}
}
|
Some guy on reddit said that cin>>current is taking next word every next time it is run. But i cant get why it is not prompting me for input every time, and why it taking next word every time it runs. Can you explain it to me pls? Tyvm
Am i retarded or something?
Ty in advance