C++ and string conversion

This is a discussion on C++ and string conversion within the C++ Programming forums, part of the General Programming Boards category; Hi there, I'm trying to implement genetic algorithms into a game I'm making. I have a problem with the way ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    30

    C++ and string conversion

    Hi there,

    I'm trying to implement genetic algorithms into a game I'm making. I have a problem with the way I'm storing and accessing information in text files.

    This is what I'm doing, basically:


    - Every enemy has an array of integers as a variable (their chromosome).

    - These arrays are stored by writing them as one line each to a text file. So it looks like:


    Code:
    1111111111   (a simple robot)
    1123112213   (a slightly more advanced one)


    - These are then accessed by being read into an array of strings (each line is one chromosome, which is one element of the array).


    And so...my problem is that I now need to rip apart these strings and get them back into arrays of integers. So that after I have stored them and read them back, I can then mutate the values and create new "evolved" enemies.

    I've just spent a dreadful couple of hours trying to figure out how I mght be able to accomplish this. The strings in C++ seem a lot less flexible than in Java, that's for sure.


    Can anyone offer any guidance?


    Much appreciated.

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    type casting would do the trick for you. for example:

    Code:
    // z is predefined array of int's
    z[0] = (int)YourCharArray[0];
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    30
    So I turn the strings into arrays of chars and then do what you've shown there, eh? Have I got that right?

  4. #4
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    a string is an array of chars. Are you having trouble reading from the file?
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    30
    Ah, no. I have used the string class in order to have actual strings instead of arrays of chars.

    But if I turn those strings to arrays of chars, I can then cast them to integers?

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    First, I hope you are using the <string> type.

    Secondly, the individual characters in a string, e.g. input[i], are char types. All char types are stored internally as their integer ascii codes (a char type is actually an int). You can look them up in an ascii table. The code for '1' happens to be 49. So, if you do this:
    Code:
    char ch = '1';
    int myCharCode = ch;
    cout<<myCharCode<<endl;
    The steps are:

    1) '1' is converted to its ascii code, which is the integer 49, and stored in ch.

    2) ch is assigned to the int type myCharCode, and then

    3) 49 is displayed.

    The trick is this: when you cout<< an int type, it will display the int directly, but when you cout<< a char type, it will assume the int stored in the variable is an ascii code, and then cout<< will covert the code to the corresponding character, and then display it.

    Since you don't want to store the ascii codes in your numeric array, as this does:

    myIntArray[i] = input[i];

    you need to parse your string of characters, and assign a value to your array depending on what char you have. You can use a switch statement to accomplish that, with the case statements being the characters '0' through '9'.
    Last edited by 7stud; 11-01-2005 at 02:31 PM.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    30
    But if the string is "1123211132", how do I actually access the characters within?

    In Java, I'd just do stringName.getChar(index)...is there a C++ way of doing that?

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Secondly, the individual characters in a string, e.g. input[i], are char types.
    ...see how much easier C++ is than Java.
    Last edited by 7stud; 11-01-2005 at 02:20 PM.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    A C++ beginner should never use casting to accomplish anything.

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    30
    Ohhh, I see. I had no idea you could do that with strings.

    You'll have to forgive me - I'm in England, and it's been a long day.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The simplest way to do this is to subtract the '0' character value from the current character in the string. This is guaranteed to be valid in any character set. So in a loop through each character in the string, it is as simple as:
    Code:
    myIntArray[i] = input[i] - '0';
    (where myIntArray is an integer array of the same size or larger than the string, input is a string, and i is some valid index from 0 to input.size() - 1).

    You might also want to consider using a vector for your int array, although if the size is always a constant it might not be all that beneficial to make the effort to switch.

  12. #12
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Onions
    - These are then accessed by being read into an array of strings (each line is one chromosome, which is one element of the array).
    why are you reading them as strings?

    use iostream and read them as an arrary of integers.

    The strings in C++ seem a lot less flexible than in Java, that's for sure.
    don't take this the wrong way, but why don't you use java then? I'm not being mean (although wandering onto a C++ board and implying it's inferior to java is not the best way to get help ) but seriously, if you're more comfortable in java, why are you using C++?
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  13. #13
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    Quote Originally Posted by 7stud
    A C++ beginner should never use casting to accomplish anything.
    Why not?

    Something like a program to convert characters to their ascii codes springs to mind, and it's a heck of a lot easier to learn/understand than a lot of other stuff in C++, pointers for example.

Popular pages Recent additions subscribe to a feed

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