For starters:
Code:
#include <iostream>
//#include <string.h> NO!
#include <string>
//char sentence; Globals are evil
int main()
{
std::string input;
std::cout << "Please enter a sentence and press Enter: ";
//std::cin >> sentence; This will stop with whitespace
std::getline(std::cin, input); //reads a line of cin into 'input'
return (0);
}
Now to count. You can't count words, but, after all, what is a word (in a string)? It's a bunch of crap bookended by spaces. In general, a sentence with n spaces has n+1 words. So, what kind of algorithm could count whitespaces?
Code:
std::count(iterator,iterator,object_type);
So...
Code:
#include <iostream>
#include <string>
#include <algorithm> //for std::count
int main()
{
std::string input;
std::cout << "Please enter a sentence and press Enter: ";
std::getline(std::cin, input);
std::cout << ( std::count(input.begin(),input.end(),' ') + 1 ) << std::endl; //Look from the beginning of the string to the end, counting the occurrences of the char ' '.
return (0);
}