Why does this word counter not work?

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.


#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
inline void 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) {
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.

Thanks for your time.
You are missing a '{' at the beginning of your while loop (I assume it is going to the second to last brace).

Btw, use code tags next time, see here for info:
http://www.cplusplus.com/articles/firedraco1/
WOW I CANT BELIEVE IT!!!!

I put the brace after the for function.... T_T!!!

Thanks for your help.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
inline void 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;
        
    }
 
}
   


The syntax thing is really helpful thanks :)


Thanks again,
You might want to look up how to organize your coding.
Topic archived. No new replies allowed.