strcat problems

This is a discussion on strcat problems within the C++ Programming forums, part of the General Programming Boards category; This doesn't work as I'd expect it to. Obviously I'm doing something wrong, but I can't see it. Please can ...

  1. #1
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124

    strcat problems

    This doesn't work as I'd expect it to. Obviously I'm doing something wrong, but I can't see it. Please can someone give me a nudge.

    Code extract:
    Code:
       char *article[] = { " the ", " a ", " some ", " any " };
       const int size = 50;
       char sentence[ size ];
       int number;
    
       for ( int i = 0; i < size; i++ )
          sentence[ i ] = ' ';
       
       for ( int i = 0; i < 20; i++ ) {
          number = rand() % 3;    
          cout << sentence << '\n';
          strcat( article[ number ], sentence );
          
          cout << sentence << '\n';
          
          for ( int j = 0; j < size; j++ )
             sentence[ j ] = ' ';
       }

  2. #2
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    Code:
    strcat( article[ number ], sentence );
    strcat(dst, src) appends src to dst, which means that dst = dst + src

    And make sure sentence contains enough space.

  3. #3
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Well, I understand what strcat does, but I need it to copy the string found at a certain location in the article[] array of strings and then place it into the sentence array. It gives me a weird error at run-time and I don't know what I'm doing wrong.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Can you explain what you're trying to achieve? Are you trying to make a sentence with 20 words?

    > const int size = 50;
    If the answer to the above question is yes, you need more than 50, maybe 80 or 100.

    > for ( int i = 0; i < size; i++ )
    > sentence[ i ] = ' ';
    If you want to erase sentence, use:
    sentence[0] = '\0';
    or:
    strcpy(sentence,"");

    > number = rand() % 3;
    If you want a number between 0 and 3, use:
    number = rand() % 4;

    > strcat( article[ number ], sentence );
    I think you want:
    > strcat( sentence, article[ number ] );

    > for ( int j = 0; j < size; j++ )
    > sentence[ j ] = ' ';
    Are you sure you want to do this within the for-loop?

  5. #5
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Aha - Brainwave! Or coffee. Either way I figured it out. Thanks for the assistence people.

    Here's the code:

    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    
    #include <cstring>
    #include <ctime>
    #include <cstdlib>
    
    int main()
    {
       char *article[] = { "the ", "a ", "some ", "any " };
       char *noun[]    = { "boy ", "girl ", "dog ", "town ", "car " };
       char *verb[]    = { "ran ", "walked ", "skipped ", "drove ", "jumped " };
       char *preposition[] = { "to ", "from ", "over ", "under ", "on " };
       const int size = 50;
       char sentence[ size ];
       int number;
       
       srand( time( 0 ) );
       
       for ( int i = 0; i < size; i++ )
          sentence[ i ] = '\0';
       
       for ( int i = 0; i < 20; i++ ) {
          number = rand() % 4;    
          strcat( sentence, article[ number ] );
          
          number = rand() % 5;
          strcat( sentence, noun[ number ] );
          
          number = rand() % 5;
          strcat( sentence, verb[ number ] );
          
          number = rand() % 5;
          strcat( sentence, preposition[ number ] );
          
          number = rand() % 4;
          strcat( sentence, article[ number ] );
          
          number = rand() % 5;
          strcat( sentence, noun[ number ] );
          
          cout << sentence << '\n';
          
          for ( int j = 0; j < size; j++ )
             sentence[ j ] = '\0';
       }   
       
       cin.get();
       return 0;
    }
    Last edited by cyberCLoWn; 04-06-2004 at 05:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  2. Question about strcat
    By Countfog in forum C Programming
    Replies: 5
    Last Post: 05-04-2008, 04:16 PM
  3. strcat and a char
    By jjacobweston in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2005, 05:10 PM
  4. strcat problems
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 08-16-2002, 07:51 AM
  5. Strcat function problems
    By s1k3 in forum C++ Programming
    Replies: 1
    Last Post: 12-14-2001, 12:20 AM

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