Concatination of chars

This is a discussion on Concatination of chars within the C++ Programming forums, part of the General Programming Boards category; I am having trouble concatenating 2 chars Code: char a = 'a'; char b = 'b'; strcat (a,b); cout << ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    22

    Concatination of chars

    I am having trouble concatenating 2 chars
    Code:
    char a = 'a';
    char b = 'b';
    strcat (a,b);
    cout  << a;
    what is the proper way of doing something like this?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can't concatenate two chars into a string of two chars unless you have a string to store it in [and strcat do not take inputs of char anyways].

    Either of these would work:
    Using C++ properly:
    Code:
    string a("a");
    string b("b");
    a += b;  // or a = a + b; if you like typing a lot. 
    cout << a;
    or standard C with a bit of C++ for output:
    Code:
    char a[3] = "a";
    char b[2] = "b";
    strcat(a, b);
    cout << a;
    Or using indexes to arrays:
    Code:
    char a[3] = "a";
    char b = 'b';
    a[1] = b;
    a[2] = 0;   // Just in case. It should really be zero from the a[3] = "a"; line.
    cout << a;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while condition question..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 04-04-2009, 04:49 PM
  2. Counting how many chars in a string
    By tigs in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 12:25 AM
  3. really got stuck with unsigned chars
    By Abdi in forum C Programming
    Replies: 7
    Last Post: 06-11-2002, 12:47 PM
  4. move chars position
    By SpuRky in forum C Programming
    Replies: 3
    Last Post: 06-09-2002, 02:59 AM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21