I wrote a program for a class assignment that is supposed to use random number generator to produce sentences. The program has to use four arrays of (pointers to char) and each chosen word must be concatenated to the previous words in an array that has to be able to hold an entire sentence. The sentence also has to begin in a capital letter and end with a period. The number of letters in the array cannot be pre-counted so that the program can be easily modifiable.

Well, I wrote the program, resolved most of the build errors, and then I got 6 occurrences of error C2664. I've tried multiple methods of resolving this and I can't figure it out (probably because this is only my second semester using C++ and it's my independent study class). Would you happen to know how to fix this? Thanks so much!

.\main.cpp(32) : error C2664: 'getSize' : cannot convert parameter 1 from 'const char *[6]' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

The code for the line (and function) are as follows:

const char* article[]={"the", "a", "one", "some", "every", "any"};
	const char* noun[]={"boy", "girl", "monkey", "LU", "car"};
	const char* verb[]={"drove", "jumped", "ran", "walked", "bit", "slithered"};
	const char* prep[]={"to", "from", "over", "under", "on"};
	sentence = article[(1 + rand()%getSize(article))] + " " + 
		noun[(1 + rand()%getSize(noun))] + " " + 
		verb[(1 + rand()%getSize(verb))] + " " + 
		prep[(1 + rand()%getSize(prep))] + " " + 
		article[(1 + rand()%getSize(article))] + " " + 
		noun[(1 + rand()%getSize(noun))] + ".";
int getSize(const char* s)
	int size;
	for (size=0; *s != '/0'; s++)
	return size;

Recommended Answers

All 5 Replies

you are attempting to pass an array of strings to getSize(), but it only expects an array of characters. getSize() is nothing more or less than your own version of the standard C library function strlen().

Thanks so much for taking the time to reply!

But strlen() gave me the exact same error when I tried that. That's why I made my own getSize(). Is there something wrong with my array? I'm not one-hundred percent sure about using arrays of pointers to char because I haven't fully understood it yet. Am I missing some key point here that would help me figure out/resolve this error?

Thank you!


strlen(article[0]); //or
getSize(article[0]); //???

> const char* article[]={"the", "a", "one", "some", "every", "any"}
Is (for the compiler)
const char* article[6]={"the", "a", "one", "some", "every", "any"}

You cannot write a function which will tell you the answer 6.

You can do this, which will give you 6 sizeof(article)/sizeof(article[0]) Which can be conveniently expressed as a macro #define ASIZE(x) (sizeof(x)/sizeof(x[0])) You then write sentence = article[(rand()%ASIZE(article))]

Thank you very much, Salem! That worked brilliantly! I applied it to the rest of the program and now have some very interesting sentences! Complete success!

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.