[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2184
  • Last Modified:

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*)â
0
mcgilljd
Asked:
mcgilljd
  • 3
  • 2
  • 2
4 Solutions
 
plusone3055Commented:
0
 
mcgilljdAuthor Commented:
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 );
                                }
                }
0
 
plusone3055Commented:
C++ does nto support arrays of mixed types
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
mcgilljdAuthor Commented:
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?
0
 
evilrixSenior Software Engineer (Avast)Commented:
Since you're using boost anyway, just use boost::lexical_cast.
http://www.boost.org/doc/libs/1_51_0/doc/html/boost_lexical_cast.html


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

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
>> Something that holds an array of mixed types?
You mean, like an array tuples?

C++ doesn't support tuples natively, but Boost can come to your rescue here...

http://www.boost.org/doc/libs/1_51_0/libs/tuple/doc/tuple_users_guide.html

...but, what's wrong with an array of structs?
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> What is considered best or most efficient way?
Don't use unsafe c functions when working with C++ types. Either use boost lexical cast or, if that's not possible, use a stringstream to perform the conversion.

One of our experts, DanRollins, has written a nice article on performing conversions.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now