Thread: space in strcat

  1. #1
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    space in strcat

    howdy,
    when using the strcat function is it possible to place a space or defined character between the strings being joined

    Code:
    #include <iostream>
    #include <string>
    int main()
    {
      char* fn;
      char* ln;
    
      	cout<<"First Name?: ";
      	cin>>fn;
      	cout<<"Last Name?: ";
      	cin>>ln;
      	
       cout<<"Name: "<< (strcat(fn, ln))<<endl;
    return 0;
    }
    for example place an asterisk between the first and last names.

    btw - this code works but gets a run time error "aborted"on g++, no idea why??

    thanks
    M.R.

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    >btw - this code works but gets a run time error "aborted"on g++, no idea why??

    you allocate no storage space of any kind for the data!

    >when using the strcat function is it possible to place a space or defined character between the strings being joined

    not really without two calls,

    use sprintf()


    int sprintf( char *buffer, const char *format [, argument] ... )
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    char fn[20];
    char ln[30];

    or use the 'new' operator to allocate space for your data.

    you could utilize the arrays and find out how long your string is and write a space over the null character and a null character in the array char. Then do your strcat()...

    or do two calls as no_one mentioned

    strcat(fn, ' ');
    strcat(fn,ln);

    or even easier

    cout << "enter your first and last name: "
    cin.getline(namearray,40,'\n');
    Blue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Out of space when compiling kernel
    By NuNn in forum Linux Programming
    Replies: 3
    Last Post: 04-01-2009, 02:43 PM
  2. Question on string concatenate (strcat)...
    By cjmdjm in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2005, 12:42 AM
  3. disk space mysteriously gone
    By evader in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2004, 01:30 PM
  4. Replies: 12
    Last Post: 05-17-2003, 05:58 AM