Thread: Strcat function problems

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    1

    Unhappy 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.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    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';
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM