C++ Primer Plus Chapter 5 Exercise 7

c plus plusThe trick to exercise 7 is that we must setup and use C’s strcmp function correctly. The problem with C++ is that when using C++’s string compare functions, you are really just comparing address’s, not the string’s themselves. To get around this we use <cstring> along with strcmp() to compare to strings.  If two string arguments in strcmp() are  identical, strcmp() returns a 0. So, we look for an instance when it is not zero. You could technically still use <string> here and get a good solution, but you may run into issues in other scenarios . Here is my solution below:

7. 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.

#include <iostream>
#include <cstring>

using namespace std;

int main()
char input[100];
int words = 0;
char compare[] = "done";

cout << "Enter words (to stop, type the word done):" << endl;
cin >> input;

cin >> input;

cout << "You entered a total of " << words << " words " << endl;

return 0;