Random Windows Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MonkeyHater
    New Member
    • Nov 2008
    • 13

    Random Windows Error

    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....

    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;
    }
    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?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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

    Comment

    • MonkeyHater
      New Member
      • Nov 2008
      • 13

      #3
      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?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Bookmark this link and search for the 'toupper()' function/macro (it is described in ctype.h or cctype)

        kind regards,

        Jos

        Comment

        • MonkeyHater
          New Member
          • Nov 2008
          • 13

          #5
          Thank you so much! That answered all my questions!

          Comment

          Working...