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

  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
    28,413
    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*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,366
    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,057
    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, 08:54 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. convert long to pointer to char array
    By gazmack in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2003, 11:33 AM
  5. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM