By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,463 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,463 IT Pros & Developers. It's quick & easy.

Random Windows Error

P: 13
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....

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     char *article[]={"the", "a", "one", "some", "any"};
  11.     char *noun[]={" boy", " girl", " dog", " town", " car"};
  12.     char *verb[]={" drove ", " jumped ", " ran ", " walked ", " skipped "};
  13.     char *preposition[]={"to ", "from ", "over ", "under ", "on "};
  14.     char sen[100];
  15.     int i, r, q, j;
  16.     j=0;
  17.  
  18.     srand(time(NULL));
  19.     while(j<=10)
  20.     {
  21.        for(i=1;i<=7;i++)
  22.        {
  23.        r=(rand()%5+1);
  24.  
  25.        if(i==1)
  26.           strcpy (sen, *(article+r));
  27.        else if(i==2)
  28.           strcat (sen, *(noun+r));
  29.        else if(i==3)
  30.           strcat (sen, *(verb+r));
  31.        else if(i==4)
  32.           strcat (sen, *(preposition+r));
  33.        else if(i==5)
  34.           strcat (sen, *(article+r));
  35.        else if(i==6)
  36.           strcat (sen, *(noun+r));
  37.        else
  38.           strcat (sen, ".");
  39.          }
  40.        q=0;
  41.        while (sen[q]!='\0')
  42.           cout <<sen[q++];
  43.        cout <<endl;
  44.        j++;
  45.     }
  46.  
  47. system("PAUSE");
  48. return 0;
  49. }
  50.  
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?
Nov 8 '08 #1
Share this Question
Share on Google+
4 Replies


Expert 10K+
P: 11,448
Indexes in C and C++ and Java and C# and a few more languages start at zero,
not at one so simply generate an index value as rand()%5 and skip adding one
to it.

kind regards,

Jos
Nov 8 '08 #2

P: 13
Wow... that fixed it all and made me feel stupid. Thank you so much!
Any ideas on how to change the first letter to a capital, other than having another string with capital letter words to start the copy into the second string?
Nov 8 '08 #3

Expert 10K+
P: 11,448
Bookmark this link and search for the 'toupper()' function/macro (it is described in ctype.h or cctype)

kind regards,

Jos
Nov 8 '08 #4

P: 13
Thank you so much! That answered all my questions!
Nov 8 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.