Thread: string to char array?

  1. #1
    Registered User
    Join Date
    Mar 2004

    string to char array?

    Hi again.

    The situation:
    string un[ not_important ];
    char username[24];
    username = un[ not_important ]; //error happens here 
    if(strcmp(input, username) == 0)
         //blah; input is a character array
    The error I get is as follows:
    incompatible types in assignment of 'string' to 'char[24]'

    I've looked around and so far I haven't come up with anything.

    ELABORATE IDEA #1! : I thought about simply making username a string, instead of a character array. But then it won't pass into strcmp().

    Any fixes?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Stick to using strings for everything unless you absolutely need a char array for something

    For example, you should be able to do this with two strings
    if ( input == username ) {
        // blah
    Remember, these are classes you're dealing with, so operator overloading applies and == should be overloaded to do the "right thing"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    You cannot assign a char-array like that. Use strcpy (for char array)and c_str (for std::string objects). By using only std::string objects would certainly make life easier.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  4. #4
    Registered User
    Join Date
    Mar 2004
    'tis been done. Thanks for the help. It works...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Character arrays
    By PsychoBrat in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2002, 12:02 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM