Link to home
Start Free TrialLog in
Avatar of mcgilljd
mcgilljd

asked on

C++ convert a string to a number

How do I convert a string to a number?


  BOOST_FOREACH(const std::vector<std::string>& row, instruments)
        {
             
                double bid_price = strtod( row.at(5) ) ;
                int bid_quantity = atoi( row.at(6) );
                double offer_price = atof( row.at(7) );
                int offer_quantity = atoi( row.at(8) );




Application.cpp:360: error: cannot convert âconst std::basic_string<char, std::char_traits<char>, std::allocator<char> >â to âconst char*â for argument â1â to
âdouble strtod(const char*, char**)â
Application.cpp:361: error: cannot convert âconst std::basic_string<char, std::char_traits<char>, std::allocator<char> >â to âconst char*â for argument â1â to âint atoi(const char*)â
Application.cpp:362: error: cannot convert âconst std::basic_string<char, std::char_traits<char>, std::allocator<char> >â to âconst char*â for argument â1â to âdouble atof(const char*)â
Application.cpp:363: error: cannot convert âconst std::basic_string<char, std::char_traits<char>, std::allocator<char> >â to âconst char*â for argument â1â to âint atoi(const char*)â
SOLUTION
Avatar of plusone3055
plusone3055
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcgilljd
mcgilljd

ASKER

Is there a better way of creating instruments than using   std::vector< std::string > rows;?

In the first place?  Something that holds an array of mixed types?



  while ((dbRow = mysql_fetch_row(dbResult)))
                {
                       std::vector< std::string > rows;
                                if (strlen(dbRow[0]))
                                {
                                       
                                        rows.push_back(dbRow[5]);
                                        rows.push_back(dbRow[6]);
                                        rows.push_back(dbRow[7]);
                                        rows.push_back(dbRow[8]);
                                        instruments.push_back( rows );
                                }
                }
C++ does nto support arrays of mixed types
I got it to work using this:

 const char * char_bid_price = row.at(5).c_str()
 double bid_price = atof( char_bid_price ) ;


What is considered best or most efficient way?
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial