0

Is there a way I can avoid using the atof function? How can I convert a string to float?

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
    double x = 0;
    for(int i=0; i<argc; i++)
    x += atof(argv[i]);
    cout<<x<<endl;
    return 0;
}
  • 1
    why would you want to avoid using a library function? – Jack Jan 25 '15 at 18:38
  • @Jack For example, because it's bad practice to use the aforementioned library function. It may exhibit undefined behavior if the input is inappropriate, it reduces the possibility of error handling, etc. – The Paramagnetic Croissant Jan 25 '15 at 18:42
  • 2
    Just to note, the first element in the argv array is the executable name. You should start at index '1' for arguments passed. – PDizzle745 Jan 25 '15 at 18:54
  • possible duplicate of [Convert string to int C++](http://stackoverflow.com/questions/7663709/convert-string-to-int-c) – sashoalm Jan 25 '15 at 19:08

3 Answers3

1

You can use stringstream.

#include <iostream>
#include <sstream>
using namespace std;

int main () {    
  int val;
  stringstream ss (stringstream::in | stringstream::out);
  ss << "120";   
  ss >> val;    
  return 0;
}
Jake0x32
  • 1,402
  • 2
  • 11
  • 18
0

For converting a string to a floating-point number in C++, it's now (since the standardization of C++11) advisable to use one of std::stof, std::stod and std::strold (where these functions had been added to the C++ standard library):

std::string s = "120.0";
float f = std::stof(s);
double d = std::stod(s);
long double ld = std::stold(s);

The primary reason to prefer these functions over those in the C standard library is safety:

  1. they don't operate on raw pointers but string objects (that also leads to a minor improvement in convenience and readability: one does not have to call c_str() over and over again when working with std::strings); and

  2. they don't exhibit undefined behavior when the conversion is impossible (they predictably throw well-documented exceptions instead).

  • *it's now advisable* by which authority and/or reason? And what do you mean with *now*? as opposed to when? – Walter Jan 25 '15 at 18:50
  • @Walter the reason is safety: 1. they don't operate on raw pointers but string objects (that also leads to a minor improvement in convenience and readability: one does not have to call `c_str()` over and over again); 2. and they don't exhibit undefined behavior when the conversion is impossible (they predictably throw well-documented exceptions instead). "Now" as opposed to before the existence and standardization of C++11 (where these functions had been added to the C++ standard library). – The Paramagnetic Croissant Jan 25 '15 at 18:52
  • Great, so add this to your answer! – Walter Jan 25 '15 at 19:01
  • @Walter I've amended my answer. – The Paramagnetic Croissant Jan 25 '15 at 19:04
0

You can use boost::lexical_cast to convert between written and numeric types in a way that's very idiomatic for C++.

#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>

using namespace std;

int main(int argc, char* argv[])
{
    double x = 0;
    for(int i=1; i<argc; i++)
    x += boost::lexical_cast<float>(argv[i]);
    cout<<x<<endl;
    return 0;
}
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180