How do I extract data?

Assume that I have a user input some data, a really good example would be from a text-based RPG that I play- Ex:

Speed: 2,779,082.2947
Strength: 52,309,610.1213
Defence: 10,226,176.8378
Dexterity: 1,057,865.8803

How would I take just the numeric portions of these calculations and store them as separate variables in a C++ program? How would this be done if the user copy-pasted them all at once?

I know that I would need to treat them as strings...Each of these fields are variable length, and always have 4 decimal places. Beyond that I never really learned this type of logic.

If someone has any type of reading regarding this sort of string separating I would be in love with you <3
are you actually going to have your user type "Speed:" then your values.. making the program search through the string for the part you want?

or are you asking for speed and strength and others? (not saying those are your inputs just using ur example.)
Last edited on
Well, in this case that is the way the data is displayed on the website, so excluding the words 'speed' etc to store the variables as "spd" "str""dex" "def" etc would seem to make a lot more sense.

This is just an example too...I'm just wondering how you would get c++ to extract just the numeric data from the string and replicate it for each variable based on the contents of the string.

Assume in the example above that that input was copy-pasted as a single chunk.... It's mind boggling to me.
Anyone help me out with this? I can't figure out what I need to read up on in order to do this...
So, for example, you store some values in a save file for your character. In the save file the contents are as follows:

1
2
3
4
str: 32
spd: 40
dex: 27
def: 16


A really easy way of making it to read in the values would actually be to make strength, speed, dexterity and defense single bytes with different values.

Str = (char)1
Spd = (char)2
Dex = (char)3
Def = (char)4

When you reach a new line (when the last character you read in was a newline character '\n'), check for the first byte, and compare it to one of those 4. Then, collect input until you reach the newline character. Then you can use "atoi" for the number input to convert for actual use.

Hope this helps!

This is just an example too...I'm just wondering how you would get c++ to extract just the numeric data from the string and replicate it for each variable based on the contents of the string.

Assume in the example above that that input was copy-pasted as a single chunk.... It's mind boggling to me.


Maybe you should store all the data in a vector of string. And then process each string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// store all strings in a vector.
// for each string


int num = 0;
std::string::iterator it = str.begin();

for ( int power = 0 ; it < str.end(); it++ )
{
        // convert char to number by subtracting '0'
        // and then multiply by power of 10
        num += (*(it) - '0' ) * pow( 10.0f , power );
        power ++ ;      // increase power
}
Topic archived. No new replies allowed.