Trying to write code to concatenate two arrays of pointers to chars (char* example[])
The program should randomly generate a sentence using 4 arrays of prepositions, nouns, adjectives, and verbs. I found a concat algorithm somewhere on the net and modified it to fit my needs but whenever i execute the program it returns 20 blank sentences. It probably has to do with scope, or I am putting my *s in the wrong places. Any help is appreciated
Heres my code:
//******************************************************************************
//*Title: Sentence Lab
//*Author: Micah Vanella Date: October 24,2007
//*The purpose of this program is to create a series of random sentences using
//* pointer strings.
//******************************************************************************
#include <iostream>
#include <cstdlib> //For srand and rand
using namespace std;
const int numSent=20; //Number of sentences to output
const int maxSentSize=128; //Maximum number of characters in the sentence
//***********************************************************FUNCTION DECLARATIONS
char* makeSentence(char sentence[], char* aArr[], char* nArr[], char* vArr[], char* pArr[], int sizeA, int sizeN, int sizeV, int sizeP);
char* concat(char* first, char* second);
void printSentence(char *sent);
//****************************************************************MAIN FUNCTION
void main(){
cout << "Welcome to the Amazing Sentence Maker 4000." << endl; //Introduction
cout << "\nPlease enter an integer for the seed value." << endl;//User input of
int seedval; // seed value
cin >> seedval;
srand(seedval); //Set the seed using the user specified seed value
//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"};
char sent[numSent][maxSentSize]; //To hold each sentence
//Find the number
int sizeA = (sizeof(article)/sizeof(*article));
int sizeN = (sizeof(noun)/sizeof(*noun));
int sizeV = (sizeof(verb)/sizeof(*verb));
int sizeP = (sizeof(prep)/sizeof(*prep));
//Print 20 sentences
for (int i=0; i<numSent; i++){
makeSentence(sent[i], article, noun, verb, prep, sizeA, sizeN, sizeV, sizeP);
printSentence(sent[i]);
}
char tempChar;
cout << "Program ended.\nPlease enter any value and press enter to exit.\n";
cin >> tempChar;
return;
}
//************************************************************************************
//*Function: makeSentence
//*Author: Micah Vanella Date: 10/21/07
//*This function creates the sentence by calling a concatenate function to add
//* each word to the "sentence" array and eventually returning the completed sentence
//*The sentence structure is the following: Article, noun, verb, preposition, article,
//* noun.
//************************************************************************************
char* makeSentence(char sentence[], char* article[], char* noun[], char* verb[], char* prep[], int sizeA, int sizeN, int sizeV, int sizeP){
*sentence=0; //Initialize the array of chars
concat(sentence, article[rand()%sizeA]);
concat(sentence, noun[rand()%sizeN]);
concat(sentence, verb[rand()%sizeV]);
concat(sentence, prep[rand()%sizeP]);
concat(sentence, article[rand()%sizeA]);
concat(sentence, noun[rand()%sizeN]);
return sentence;
}
//**************************************************************************
//*Function: concat
//*Author: Micah Vanella Date: 10/21/07
//*This function concatenates two strings passed in. It adds the src string
//* to the end of the dst string. It places a space between the two words.
//**************************************************************************
char* concat(char* dst, char* src){
while (*dst){ //get to the end of the dst string
++dst;
}
*dst++ = ' '; //add a space between the words then increment
do{
*dst++ = *src; //copy the src string to the end of the dst string
}
while (*src++);
return dst;
}
//**************************************************************************
//*Function: printSentence
//*Author: Micah Vanella Date: 10/21/07
//*This function takes a sentence string, changes the first character to a
//* capital, then adds a period at the end and sends it to the display.
//**************************************************************************
void printSentence(char *sent){
toupper(*sent);
while(*++sent);
*sent++ = '.';
*sent = 0;
cout << sent << endl;
return;
}