How do I convert a C char* into a C++ char?

This is a discussion on How do I convert a C char* into a C++ char? within the C++ Programming forums, part of the General Programming Boards category; How do I convert a C char* into a C++ char? Any help is highly appreciated....

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    Arrow How do I convert a C char* into a C++ char?

    How do I convert a C char* into a C++ char?

    Any help is highly appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,127
    How do I convert a C char* into a C++ char?
    Perhaps you mean to ask how to convert a char* to a std::string? If that's the case, then you would use the std::string constructor that accepts a const char*
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    2
    Thanks for your help!

    I've just found a way. I use a temporary string "dept", and it works!

    getline(in, data[count]);
    dept = data[count];
    department = dept[0];
    out << department << endl;
    count++;

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    getline() accepts C++ style strings, so that is a lot of extra work you are going through .

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Actually, it looks like thiago_j is using the string version of getline.

    Regardless, there is no need for a temporary string there. data[count][0] is the same as your use of dept. You can use out << data[count][0] << endl.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You don't need a (named) temporary string.
    Code:
    a_function_that_requires_a_string(std::string(a_char_array));
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 11:28 PM
  4. convert long to pointer to char array
    By gazmack in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2003, 12:33 PM
  5. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 12:11 PM

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