:eek: I have been working on this for hours now... I am trying to enter a string and then count the words in the string. Only problem is my book is no more than a paper weight, and other online areas are kinda confusing. Can someone point me in the right direction... I am getting a ton of errors when I compile, and this looks like the right way according to my book... Am I missing a include or something??? Any help would be appreciated... Thanks

**********************************************
# include <iostream.h>
# include <iomanip.h>
# include <string.h>

int main ()
 {
   char s[80];
   int max_words = 20;    // Maximum number of words.
   char wds[max_words]; // Words stored here.

   int num_words = 0; // Counts words as they are found
   int index = 0;     // Position along the string
   int x;             // Temporary variable
   int done = 0;      // Indicates when finished

    cout << "Please enter a short sentence:  "<<endl<<endl;
    cin.getline (s, 80);

   do
    {
     x = s.find(" ");
     num_words++;
     if (x == string::npos)
      { 
       wds[num_words] = s.substr(index);
       done = 1;
      }
     else
      {
       // Extract just the characters for the word
       wds[num_words] = s.substr(index,x-1-index);
       index = x + 1;
      }
    }while (done != 1);

   cout << "The words are:  ";
   for (x = 1; x <= num_words; x++)
   cout << wds[x] << endl;

   return 0;
 }

Recommended Answers

All 2 Replies

Well, my first inclination for such a toy program would be:

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main()
{
  string line;

  cout<<"Enter a line of text: ";
  getline(cin, line);

  int n = count(line.begin(), line.end(), ' ');
  cout<<"There are "<< n + 1 <<" words"<<endl;
}

That aside, your code is trying to mix old style C++ with new style C++. That path leads to problems. For example, you're using an array of char as if it were a string class, which won't work. You're also trying to use a non-const value as the size to an array, which also won't work.

Rather than try to fix a very broken program, I'll suggest an alternative using modern C++:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
  string line;

  cout<<"Enter a sentence: ";
  getline(cin, line);

  istringstream in(line);
  vector<string> words;
  string word;

  while (in>> word)
    words.push_back(word);

  cout<<"You typed "<< words.size() <<" words\n";
  cout<<"The words are:\n";
  for (vector<string>::const_iterator it = words.begin(); it != words.end(); ++it)
    cout<< *it <<endl;
}

The nice thing about C++ is that if it's more convenient to treat a string as an input or output stream, you can do it. :) That saves you the trouble of coding it all manually, which is error prone:

#include <cctype>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
  string line;
  vector<string> words;

  cout<<"Enter a sentence: ";
  getline(cin, line);

  for (string::size_type i = 0; i < line.length(); i++) {
    while (i < line.length() && isspace(line[i]))
      ++i;

    string word;

    while (i < line.length() && !isspace(line[i])) {
      word.push_back(line[i]);
      ++i;
    }

    words.push_back(word);
  }

  cout<<"You typed "<< words.size() <<" words\n";
  cout<<"The words are:\n";
  for (vector<string>::const_iterator it = words.begin(); it != words.end(); ++it)
    cout<< *it <<endl;
}

I am not sure about that book, it seems to have alot of program info and terms that I never see in any other code lol :) I have complained to the teacher about the book, but didn't get much back from him... Anyways, I appreciate the help. I will try and strip it apart to full understand it. Thanks for getting me going forward.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.