strcat makes program crash

This is a discussion on strcat makes program crash within the C++ Programming forums, part of the General Programming Boards category; //RANDOM SENTENCE GENERATOR USING STRCAT //I added the strcat command the program crashes.. //This program is suppossed to genterate random ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    3

    strcat makes program crash

    //RANDOM SENTENCE GENERATOR USING STRCAT

    //I added the strcat command the program crashes..
    //This program is suppossed to genterate random sentences..
    //My teacher says that we HAVE TO USE STRCAT to catonate the sentences..
    //IN CLASS MY PROFESSOR COULDNT GET THE PROGRAM TO RUN EITHER



    #include<iostream>
    #include<ctime>
    #include<cstdlib>
    #include<string.h>// I also tried <cstring>


    using std::cout;
    using std::cin;
    using std::endl;

    int main()
    {



    char *art[ 5 ] = {"The", "This", "That", "Any", "A"};
    char *adj[ 10 ] = {"skinny", "hairy", "scary", "ugly", "bald",
    "big", "small", "tall", "long", "pretty",};
    char *noun[ 8 ] = {"girl", "house", "car", "toy", "man", "woman",
    "cat", "brother"};
    char *verb[ 10 ] = {"went", "jump", "skip", "ran", "swam",
    "moves", "walked", "talked", "hopped", "swirved"};
    char *prep[ 7 ] = {"with", "at", "by", "in", "to", "from", "under"};

    srand (time(0));

    int a = rand() % 5;
    int b = rand() % 10;
    int c = rand() % 8;
    int d = rand() % 10;
    int e = rand() % 7;

    char *s = " ";
    char *s1 = art[ a ];
    char *s2 = adj[ b ];
    char *s3 = noun[ c ];
    char *s4 = verb[ d ];
    char *s5 = prep[ e ];
    strcat(s1,s);//THIS IS WHAT MAKES THE PROGRAM CRASH
    //cout <<s1<<s<<s2<<s<<s3<<s<<s4<<s<<s5<<s<<s1<<s<<s2<<s< <s3<<".\n";

    return 0;
    }

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    >>//IN CLASS MY PROFESSOR COULDNT GET THE PROGRAM TO RUN EITHER

    1 - Get a new professor.
    2 - You're going over the buffer. You're concatenating 5 elements into an array of anywhere from 0 to 4 elements.
    3 - From what you've posted you aren't dynamically allocating your arrays even though that's what you would be doing in the scope of this program.
    4 - Get a new professor.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    3

    strcat program crashes

    what can i do to run the program??

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Like this

    char s[1000];
    strcpy( s, art[ a ] );
    strcat( s, adj[ b ] );

    etc

    then

    cout << s;

    Man, the quality of teaching is in free fall...

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    3

    thanks

    thanks..i guess i need a new professor..i hope i can pass the next c++ I a might not be properly prepared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my program crash?
    By vrkiller in forum C++ Programming
    Replies: 10
    Last Post: 03-04-2009, 02:03 PM
  2. Replies: 11
    Last Post: 08-05-2008, 01:42 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  4. Program crash ?!
    By tilex in forum C++ Programming
    Replies: 6
    Last Post: 12-21-2004, 07:47 PM
  5. hOW TO MAKE A PROGRAM WHICH MAKES MY pC TO SHUTDOWN?
    By ALANAIR23 in forum Windows Programming
    Replies: 9
    Last Post: 09-01-2001, 01:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21