• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2369
  • 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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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