-
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.
-
>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] ... )
-
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');