error converting string to char array

This is a discussion on error converting string to char array within the C++ Programming forums, part of the General Programming Boards category; Can anyone tell me why the folowing is getting on error. string orig = 123456789 string newSSN; newSSN = orig.substr(0, ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    26

    error converting string to char array

    Can anyone tell me why the folowing is getting on error.

    string orig = 123456789
    string newSSN;
    newSSN = orig.substr(0, 3) + "-" +
    orig.substr(3, 2) + "-" +
    orig.substr(5, 4);

    char * newString = new char[strlen newSSN)+1];

    ERROR = cannot convert `newSSN' from type `string' to type `const char *'

    I am trying to convert string newSSN to a char array ,newString, but I keep getting that error.

    Thanks,

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Code:
    string orig = "123456789"; //missing semicolon and "
    //Prefer
    //string orig("123456789");
    Code:
    char * newString = new char[strlen (newSSN.c_str())+1];
    strlen accepts char * and not string. For more information check out this and this
    Last edited by ripper079; 07-11-2003 at 10:59 AM.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    148
    Code:
    char * newString = new char[newSSN.size() + 1];

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    26
    Thanks,

    I added that code and was able to compile and run it but I had expected to see the value in newSSN also in newString. Instead newString was empty. Any thouhgts on that.

    Maybe I am on the wrong track.

    Thanks for any help you may offer.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    161
    Code:
    char * newString = new char[newSSN.size() + 1];
    strcpy(newString, newSSN.c_str());

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    148
    Originally posted by thefroggy
    Code:
    char * newString = new char[newSSN.size() + 1];
    strcpy(newString, newSSN.c_str());
    Better..
    Code:
    char *newString = new char[newSSN.size() + 1];
    newSSN.copy(newString,string::npos);
    newString[newSSN.size()] = 0;

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    26
    Thanks for all your help. It worked.

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, 10:42 PM
  2. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  4. Character arrays
    By PsychoBrat in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2002, 01:02 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 12:57 PM

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