This is a code out of a book i have started reading, maybe some of you might recognize it. But the problem is i have the code EXACTLY as i am seeing it in the book (if not then wow) and it does not perform its output commands (so i dont even know if its incrementing anything and clearly and probably it isnt.
This is out of one of author and programmer, Stroustrup's (Im sure you all recognize this name :P) beginners book.
int main ()
{
int number_of_words = 0;
string previous = " "; //not a word
string current;
while(cin>>current)
++number_of_words; //increase word count
if(previous == current) {
cout << "Repeated word alert!: " << current
<< endl
<< "You have typed " << number_of_words << " words!"
<< endl
<< "If this is an error, you should correct it!";
previous = current;
}
}
I tried a few different things, and i know i need to understand the fundamental logic behind my mistake and how it SHOULD work, so thats what i am looking for a straight answer rather then sitting here and explaining all of the things ive tried and blablabla.
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iostream>
usingnamespace std;
inlinevoid keep_window_open() { char ch; cin>>ch;}
int main ()
{
int number_of_words = 0;
string previous = " "; //not a word
string current;
while(cin>>current) {
++number_of_words; //increase word count
if(previous == current) {[b] <-----------Here is where I made the mistake.
cout << "Repeated word alert!: " << current
<< endl
<< "You have typed " << number_of_words << " words!"
<< endl
<< "If this is an error, you should correct it!";
previous = current;
}
}