-
Strcat function problems
Ok, Im having a problem here. I have the user type a string in which goes to char string[10]; I then do a for loop on the string bypassing if a space is found and if a letter is found i wanted to copy the letter to another string result[10]; my code is strcat(result, string[x]) where x is the for loops timer from 0 -9. I get an error saying something about const char and pointer and how i cant use it.... It seems to me like cat only works on strings and you are not allowed to add letters to the end of a string. Please help me.
-
Exactly. You cannot add a single character to a string with strcat. The string class in <string> is a lot easier to use. You can add it directly to the string by using the assigning to the index, and then putting the '\0' at the spot after.
char name[10] = "Joe";
name[3] = 'y';
name[4] = '\0';
/* name now Joey */
but it's a lot of work.
Using std::string, it's like this.
std::string name = "Joe"; // notice no length needed
name += 'y';