How do I get a C++ program to count the number of words as a user enters input?
<p>I'm trying to write a program that keeps taking input from the user until the user enters "quit." Each time the user enters input, I want the program to print out the number of words the user has entered. So the following input on the user's part:</p>

<pre><code>hello how are you
</code></pre>

<p>would yield the following output:</p>

<pre><code>You entered 4 words.
</code></pre>

<p>However, I am having trouble writing the program so that it counts the number of words on just one line; it doesn't clear the number before going onto the next line. So, if it took input from the user three times, it would add up the total number of words on those three lines. For example, the following input:</p>

<pre><code>how are you
i am good thank you
quit
</code></pre>

<p>would yield the following output:</p>

<pre><code> You entered 9 words.
</code></pre>

<p>when I want it to output the number of words following each line the user enters (except quit), i.e.</p>

<pre><code>&gt;&gt;how are you
&lt;&lt;You entered 3 words.
&gt;&gt;i am good thank you
&lt;&lt;You entered 5 words.
&gt;&gt;quit
</code></pre>

<p>Here's the relevant bit of my code:</p>

<pre><code>char *input;
int inum;

int inputLoop()
{
    char quit[] = "quit";
    inum = 0; //counts number of words

    while (strcmp(input, quit) != 0)
    {
         cin &gt;&gt; input;
         inum++;
    }
    cout &lt;&lt;"You entered " &lt;&lt;inum &lt;&lt;" words." &lt;&lt;endl;
</code></pre>

<p>I'd rather not use something like a vector; whatever I use will need to be converted to a *char eventually because my global variable is a *char. (And my global variable is a *char because, depending on certain conditions, *input may be set to *argv[] from main.)</p>

<p>I've tried all sorts of things, but I just can't seem to get past the fact that strcmp(input, quit) compares one word of the input at a time to quit rather than comparing the entire input line to quit. HELP.</p>
