: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;
}