This program is a random sentence generator. I have it running.. more or less. When it complies I will get some correct sentences and I will get some sentences that don't even contain real words. Also each time it compiles I get a Windows error that says that it has to close the program, random when it happens. The program may get 5 sentences in and then get the error or it won't get any and get the error. The code is....
Any tips would be greatly appreciated. The error and non words are my main concern, but I have a minor secondary question. How would I get the first letter in the sentence to be capital?
Code:
#include <iostream> #include <cstring> #include <cstdlib> #include <ctime> using namespace std; int main() { char *article[]={"the", "a", "one", "some", "any"}; char *noun[]={" boy", " girl", " dog", " town", " car"}; char *verb[]={" drove ", " jumped ", " ran ", " walked ", " skipped "}; char *preposition[]={"to ", "from ", "over ", "under ", "on "}; char sen[100]; int i, r, q, j; j=0; srand(time(NULL)); while(j<=10) { for(i=1;i<=7;i++) { r=(rand()%5+1); if(i==1) strcpy (sen, *(article+r)); else if(i==2) strcat (sen, *(noun+r)); else if(i==3) strcat (sen, *(verb+r)); else if(i==4) strcat (sen, *(preposition+r)); else if(i==5) strcat (sen, *(article+r)); else if(i==6) strcat (sen, *(noun+r)); else strcat (sen, "."); } q=0; while (sen[q]!='\0') cout <<sen[q++]; cout <<endl; j++; } system("PAUSE"); return 0; }
Comment