I think I'm on the right track, basically trying to count the number of tokens but it keeps telling me cannot convert to a const.

//This program is made to count the number of words. 

#include <iostream> 
using namespace std; 
#include <cstring> 

int wordCount(char *); 

int main()
{
	char checkCount[20]; 
	cout << "Enter a sentence: " ; 
	cin.getline(checkCount, 21, '\n'); 

	cout << "The sentence has " << wordCount(checkCount) << " word." << endl; 

	return 0; 
}

int wordCount(char yourSentence)
{
	int count = 0;
	char* tokenPtr;
	tokenPtr = yourSentencetok(yourSentence, " ");
	
	while (tokenPtr!='NULL')
	{
		count++; 
	}
	return count; 
}

Recommended Answers

All 2 Replies

>>while (tokenPtr!='NULL')

Remove the quotes around NULL. while (tokenPtr!=NULL) >>char checkCount[20];
You should make that a lot bigger. A 20 character sentence is pretty darned short.

commented: Great help, thanks Ancient Dragon +1

Thanks, yeah I had it a bit short for testing.

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.