Word Counter

Write a program that uses an array of char and a loop to read one word at a time until the word done is entered. The program should then report the number of words entered(not counting done).

A sample run could look like this:
Enter words (to stop, type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7 words.

You should include the cstring header file and use the strcmp() function to make the
comparison test.

I can only utilize loops since it was covered in this chapter I managed to get this. I felt like using totalwords-1 at the end seemed like cheating. Is there a better way to do this without having to do what I did at the end to have the counter not count the word Done

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

int main()
{
int totalwords=0;
string input;

cout<<"When you are done type Done"<<endl;

for(int i=0;input!="Done";i++){
    cin>>input;

totalwords++;

}
cout<<totalwords-1;
}
What you did in the end is no big deal however
The requirements said to use array of char, you are using string...
oh right lol I forgot that the next question asked to use a string object and a relational expression I accidently posted this instead
Topic archived. No new replies allowed.