1. 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. type casting would do the trick for you. for example:

Code:
```// z is predefined array of int's
z[0] = (int)YourCharArray[0];```

3. So I turn the strings into arrays of chars and then do what you've shown there, eh? Have I got that right?

4. a string is an array of chars. Are you having trouble reading from the file?

5. 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. 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'.

7. 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. Secondly, the individual characters in a string, e.g. input[i], are char types.
...see how much easier C++ is than Java.

9. A C++ beginner should never use casting to accomplish anything.

10. 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. 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. 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++?

13. 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.