I'm attempting to copy one character (string array index 0) to another string array with 30 indexes.
This is a program to generate a username from two string arrays. firstName and lastName. The format of the username needs to be: jsmith (eg: the user enters john smith. the output should be jsmith.)
Here's what I have.
void generateName(char firstName[15],char lastName[15])
{
char userName[30];
strcpy(userName, firstName[0]);
strcpy(userName, lastName);
cout << "Your newly generated username is: " <<userName <<endl;
getch();
}
I'm getting these errors:
Cannot convert 'int' to 'const char *' Type mismatch in parameter '_src' in call to 'strcpy char *,const char *)' Parameter 'firstName' is never used
Any help would be greatly appreciated.
Because you need to use
strcpy(userName, &firstName[0]);
instead. Parameters to strcpy
need to be pointers, you were passing value of first character in the char array instead.
But that call above to strcpy
will copy the whole firstName
- up to and including the null terminator. If you only want to copy first letter you can do:
memcpy(userName, &firstName[0], 1);
strcpy(&userName[1], lastName); // append last name
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.