I am working on a project to demonstrate the use of strings without using "strings" in the formal definition, as in using pointers to arrays of characters.
The program is supposed to pick a string from an array of pointers to chars, but allow the program to include more words in the array if just typed in the declaration and recompiled. It uses the rand() function to pick a word from the array, which the program then concatenates to other randomly picked words to form a sentence. The problem I am having is I need to dynamically determine the number of elements in the array in order to use the modulus to pick the random word. Here is some of my code:
//Articles, nouns, verbs, and prepositions declarations
char* article[] = {"the", "a", "one", "some", "every", "any"};
char* noun[] = {"boy", "girl", "monkey", "LU", "car"};
char* verb[] = {"drove", "jumped", "ran", "walked", "bit"};
char* prep[] = {"to", "from", "over", "under", "on"};
Then I made a little debug area to test,
I found a small algorithm online and I was trying to use it but whenever I use it it returns a value of 1 no matter what.
int findCard(char *someArray[]){
int elemNum = (sizeof(*someArray)/sizeof(someArray));
//Determine the number of elements in the array by dividing the
// total byte size of the array by the byte size of each element.
return elemNum;
}
So i made the debug area to test it:
//Find the number
int sizeA = findCard(article);
int sizeN = findCard(noun);
int sizeV = findCard(verb);
int sizeP = findCard(prep);
//For testing purposes only
cout << "sizeA = " << sizeA << endl;
cout << "sizeN = " << sizeN << endl;
cout << "sizeV = " << sizeV << endl;
cout << "sizeP = " << sizeP << endl;
cout << article[rand()%sizeA] << endl;
cout << noun[rand()%sizeN] << endl;
cout << verb[rand()%sizeN] << endl;
cout << prep[rand()%sizeN] << endl;
But like I said before it always returns 1 for every cardinality. What am I doing wrong?