In the following code I am outputting a sentence in the form:
"The <noun><verb> the <noun>."
The user enters 10 verbs and 10 nouns.
I use rand and srand to generate random sentences, but I can't use the same
noun twice in the same sentence.
Can someone tell me how to go about doing this?
Should I use a for loop and a temp variable?
How do I distinguish between one noun and the other?
Code:srand(time(NULL)); // Seed rand() char sentence[maxSentence+1] = "";// Initialize sentence to null string. //sentence output in the format "The <noun><verb> the <noun>." for (int k = 0; k < numSentences; k++) { strcat(sentence,upperThe); // The strcat(sentence,space); // (space) strcat(sentence, noun[(rand()%numWords)]->getNoun()); // random noun strcat(sentence,space); // (space) strcat(sentence, verb[(rand()%numWords)]->getVerb()); // random verb strcat(sentence,space); // (space) strcat(sentence,lowerThe); // the strcat(sentence,space); // (space) strcat(sentence, noun[(rand()%numWords)]->getNoun()); // random noun strcat(sentence,period); // (period) cout << sentence << endl; sentence[0] = '\0'; // Reset sentence to null string for next iteration. }




Reply With Quote