I've got a database filled up with doubles like the following one:
1.60000000000000000000000000000000000e+01
Does anybody know how to convert a number like that to a double in C++?
Is there a "standard" way to do this type of things? Or do I have to roll my own function?
Right now I'm doing sth like this:
#include <string>
#include <sstream>
int main() {
std::string s("1.60000000000000000000000000000000000e+01");
std::istringstream iss(s);
double d;
iss >> d;
d += 10.303030;
std::cout << d << std::endl;
}
Thanks!
Something like this? This would be the "C++" way of doing it...
#include <sstream>
using namespace std;
// ...
string s = "1.60000000000000000000000000000000000e+01";
istringstream os(s);
double d;
os >> d;
cout << d << endl;
Prints 16.
You want the standard c function atof ([A]SCII to [F]loat, but it actually uses doubles rather than floats).
Related
I am practicing input-output, I read two vectors from files f1 and f2, they are shown in the figure.
I want to use the content of two files for the output file's name. So the code is
#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
using namespace std;
int main()
{
vector<double> f1Series;
vector<double> f2Series;
double tmp;
std::ifstream input;
input.open("Input/f1.txt");
while (input >> tmp) f1Series.push_back(tmp);
input.close();
input.open("Input/f2.txt");
while (input >> tmp) f2Series.push_back(tmp);
input.close();
std::ostringstream oss1;
std::ostringstream oss2;
oss1 << f1Series[1];
oss2 << f2Series[1];
std::string f1 = oss1.str();
std::string f2 = oss2.str();
std::ofstream st("Output/Results" + f1 + "_" + f2 + ".txt");
}
However, the current result is
Results1e07_0.001000.txt
What I want is
Results1e7_1e-3.txt
Try std::scientific
double c = 1.0e-10;
std::cout << std::scientific; //setting output format
std::cout << c;
The output will be
1.00000e-010
The printf way, easy to remember if you are used to C, but definitely C-ish and old fashioned:
char temp[16];
snprintf(temp, sizeof(temp), "Results%.0e.txt", f1Series[1]);
std::ostream st(temp);
The C++ way using scientific and setprecision:
std::ostringstream oss;
oss << "Results" <<std::scientific << std::setprecision(0) << f1Series[1] << ".txt";
std::string filename = oss.str();
Remember the lines you read as strings, not converted to double. You'll need to later convert to double when you are ready to use it as a number, but you are remembering the characters you actually scanned so you can reproduce that in the file name.
vector<string> f1Series;
vector<string> f2Series;
For a project im working i want to grab text before the space and after the space.
to find the space i use the isspace method, any ideas how to get this done
Like 0x499602D2 noted in the commments, you can use std::stringstream to read from string just as you would do with any other stream (like std::cin):
#include <sstream>
#include <iostream>
#include <string>
int main()
{
int a, b;
std::stringstream ss(std::stringstream::in | std::stringstream::out);
ss << "12 14";
ss >> a;
ss >> b;
std::cout << a << std::endl;
std::cout << b << std::endl;
return 0;
}
Nice thing about this is that if you know how to work with std::cin or any other stream, you know how to work with stringstream, so you can adapt this example for you needs.
I've been trying to find the solution for this all day! You might label this as re-post but what I'm really looking for is a solution without using boost lexical cast. A traditional C++ way of doing it would be great. I tried this code but it returns a set of gibberish numbers and letters.
string line;
double lineconverted;
istringstream buffer(line);
lineconverted;
buffer >> lineconverted;
And I alse tried this, but it ALWAYS returns 0.
stringstream convert(line);
if ( !(convert >> lineconverted) ) {
lineconverted = 0;
}
Thanks in advance :)
EDIT: For the first solution I used (gibberish).. Here's a snapshot
#include <sstream>
int main(int argc, char *argv[])
{
double f = 0.0;
std::stringstream ss;
std::string s = "3.1415";
ss << s;
ss >> f;
cout << f;
}
The good thing is, that this solution works for others also, like ints, etc.
If you want to repeatedly use the same buffer, you must do ss.clear in between.
There is also a shorter solution available where you can initialize the value to a stringstream and flush it to a double at the same time:
#include <sstream>
int main(int argc, char *argv[]){
stringstream("3.1415")>>f ;
}
Since C++11 you could use std::stod function:
string line;
double lineconverted;
try
{
lineconverted = std::stod(line);
}
catch(std::invalid_argument)
{
// can't convert
}
But solution with std::stringstream also correct:
#include <sstream>
#include <string>
#include <iostream>
int main()
{
std::string str;
std::cin >> str;
std::istringstream iss(str);
double d = 0;
iss >> d;
std::cout << d << std::endl;
return 0;
}
If you want to store (to a vector for example) all the doubles of a line
#include <iostream>
#include <vector>
#include <iterator>
int main()
{
std::istream_iterator<double> in(std::cin);
std::istream_iterator<double> eof;
std::vector<double> m(in,eof);
//print
std::copy(m.begin(),m.end(),std::ostream_iterator<double>(std::cout,"\n"));
}
I want to copy the contents of a float to a string in C++. This doesn't work.
#include <iostream>
#include <sstream>
using namespace std;
int main() {
float ans = getFloat();
stringstream ss;
string strAns;
ss >> ans;
strAns = ss.str();
cout << strAns << "\n"; // displays "0"
return 0;
}
How do I do this?
I think
ss>>ans;
should be
ss<<ans;
Edit:
As James Kanze noted, you are better off using std::ostringstream instead of std::stringstream as you are not using the bidirectional functionality of the first one. This way the compiler would also throw an error that you extracting ans from the string instead of inserting it into the string.
ss << ans; instead of ss >> ans and it will work
To work with stringstreams, you have to use the PUT TO operator( << ), with an object on the right hand side. That will convert the operator to a string(if the operator is defined for the particular type)(this operator<< is already defined for a stringstream object with float object).
Then, convert the string stream to a string.. and you will have successfully converted the object to string.
As the other answers show, it should be ss << ans, since << is used for ostreams and >> is used for istreams.
If you want just to print the float to cout, you can of course avoid the detour and just write std::cout << ans;, but I guess you want to use the string otherwise.
You should however be aware of the simplifications provided by Boost's and C++11's libraries:
#include <iostream>
#include <string> //for std::string and std::to_string
#include <boost/lexical_cast.hpp>
using namesapce std;
int main() {
float ans=getFloat();
string strAns1 = boost::lexical_cast<string>(ans); //boost way
auto strAns2 = std::to_string(ans); //C++11 way
cout << "boost: " << strAns1 << "\n"
<< "C++11: " << strAns2 << "\n";
}
You are using wrong operator:
#include <iostream>
#include <sstream>
using namespace std;
int main() {
float ans=getFloat();
stringstream ss;
string strAns;
ss << ans;
strAns=ss.str();
cout<<strAns<<"\n"; // displays "0"
return 0;
}
Just one line wrong here by the look of it. You need to stream the float into the stringsteram like this:
ss << ans;
Use
strAns = std::to_string(ans);
How do I convert a string value into double format in C++?
If you're using the boost libraries, lexical cast is a very slick way of going about it.
Use stringstream :
#include <sstream>
stringstream ss;
ss << "12.34";
double d = 0.0;
ss >> d;
You can do with stringstream.
You can also catch invalid inputs like giving non-digits and asking it to convert to int.
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
stringstream s;
string input;
cout<<"Enter number: "<<endl;
cin>>input;
s.str(input);
int i;
if(s>>i)
cout<<i<<endl;
else
cout<<"Invalid input: Couldn't convert to Int"<<endl;
}
If conversion fails, s>>i returns zero, hence it prints invalid input.