Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
I've to copy elements of a string array to a Structure.

My array is declared as follows:
irr::core::array<irr::core:stringc> result:;

My Structure is declared as follows:

C#
struct playerData
{
 int id,matches,innings,notouts,runs,highest,hundred,fifty,catches,strikerate,balls,runs_given,wickets,fivewkt;
    char bestbowling[32];
};


How to copy the array elements to the structure elements?
Posted

As I've no idea what interface your array has and what order the strings come out of the array I'm going to assume that your strings are std::string and your array is std::vector<std::string xmlns:std="#unknown">.

The first thing to do is give your structure a constructor:

playerData::playerData( const std::vector<std::string> &input )
{
}


Once you've done that the problem is to convert a string to an integer. Write a function for that:

int string_to_int( const std::string &str )
{
    int value = 0;
    std::istringstream in_str( str );
    in_str >> value;
    if( !in_str )
    {
        throw std::runtime_error( "Couldn't convert string to integer" );
    }
}


and finally implement your constructor in terms of that function:

playerData::playerData( const std::vector<std::string> &input ) : 
    id( string_to_int( input[ 0 ] ) ),
    matches( string_to_int( input[ 1 ] ) ),
    // etc, one initialisation per member
{
}


Cheers,

Ash

PS: This is particularly braindamaged in some respects. What I'd be tempted to do would be to implement a constructor from a stream and pass the data in like that. It'd be easier to test and you could enter player data from the command line as well.
 
Share this answer
 
What was wrong with the answers you got in the forum[^]?

And why did you remove that message? There is no point in asking questions if you don't even bother trying to understand the answers.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900