Thread: How do i convert a string to a character array?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    20

    How do i convert a string to a character array?

    How do i convert a string to a character array?

    We are using a command line prompt like this:
    int main(int argc, char **argv)

    We put the argv into a string variable like this:
    ipstring = argv[1];
    so we could parse the string using string member functions.

    We tried an atoi on ipstring but atoi would not work we got:

    error C2664: 'atoi' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

    Any help would be greatly appreciated.
    Susan

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Use the c_str() member function.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    20
    tried this
    cout << "/n this is chunk1 = " << ipchunk1.c_str() << endl;

    Execution/run time error:
    the instruction at 0x00422820 referenced memory at
    0x00000000. The memory could not be "read."

    i like string but i don't know how to convert it to any other data type.
    how can i convert from string to char and how can i convert to integer?

    thanks,
    susan

    PS I am trying to help my son who is trying to write code just for his own interest. he hopes to take computer classes next fall when he will be a freshman in college.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    http://www.cppreference.com/cppstring.html

    Here is a good reference for the string class.
    Also in that line of code you posted, the /n should be \n

  5. #5
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96
    Code:
    #include <string>
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    
    int main() {
      string sStr = "1234";
      int iNum = atoi(sStr.c_str());
      cout << iNum << endl;
      return 0;
    }
    This works fine under Visual C++. Substitute "1234" by an argument you need to convert.
    Please excuse my poor english...

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    20
    I think your problem is connected with the fact that you can't do '=' with 0-terminated strings outside of declarations.

    There are at least 4 different common string "systems" in C++ which I identified in my notes by which include file you need:

    // cstring standard header -- not sure what this is
    #include <string.h>

    // Microsoft.
    string.h - declarations for string manipulation functions

    <string> classname is basic_string
    //I think this is a more elaborate and usefule string class

    <cstring>
    //I think this is the char* 0-terminated strings you're looking for

    FACTS about cstrings
    1: array size declared must be const;
    const int max=50; char ar[max];

    2: arrays don't allow '=' outside of decl,
    char ar[]="hello"; ok but later ar="goodbye" not ok; but cin >>ar OK

    3: in strcpy(targ,srce) targ must be ar[] with known size
    in strtok(inString,del) inString must be ar[] with known size

    4: ptrs allow = assignments after declare: via "literal" or otherwise
    therefore, function outputs must always be ptrs

    5: after decl: ptr '=' X makes alias; actions on ptr change X
    but a later ptr'='Y make Y-alias, cancels X-alias

    6. no '+' for concat, i.e. NOT s="banana"+"banana"

    FUNCTIONS: STRCAT, STRNCAT, STRCMP, STRNCMP, STRCPY, STRNCPY, STRLEN, STRTOK,

    Good luck, Al

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    <cstring> includes string.h

    Back when there was just C, the string.h header file was made up with all those nice char *(aka strings) manilplation functions.
    Now there is C++ and there is the STL string class.
    The name cstring comes from the fact they were string's for C.

    #include <cstring> or #include <string.h>
    for the functions STRCAT, STRNCAT, STRCMP, STRNCMP, STRCPY, STRNCPY, STRLEN, STRTOK,
    #include <string> to use the string class

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    20
    thank you all very much. I miss PL1. that is how old of a programmer i am!
    C++ - so many ways to do something that should be simple.
    well, my son's program is running thanks to you all.
    Happy New Year!
    Susan

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Now that the problems solved and I can no longer help, I just want to point out, Borland's manuals give excellent instructions on command-line parameters if anyone elkse has questions relating to the subject.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Navigating a character string array from pointers
    By Boxknife in forum C Programming
    Replies: 3
    Last Post: 04-16-2009, 01:32 PM
  2. How to put a string in a 2 dimensional character array?
    By atif7865 in forum C Programming
    Replies: 2
    Last Post: 12-05-2008, 10:26 PM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM