I'm trying to retreive from stdin two float numbers separated by characted "e" in C/C++.
The input is: 4.2e3
When I code
#include <cstdio>
int main(){
float a, b;
scanf("%fe%f", &a, &b);
printf("%f %f", a, b);
return 0;
}
I get 4200.000000 0.000000
, but when I change character "e" both in input and in the code then I get 4.200000 3.000000
which is the expected result.
I solve this problem using string, split and casting but it is very unclean for me. There is the code I wrote
#include <iostream>
#include <bits/stdc++.h>
#include <boost/algorithm/string.hpp>
#include <string>
using namespace std;
int main(){
float a, b;
string line;
vector<string> numbers;
cin >> line;
boost::split(numbers, line, boost::is_any_of("e"));
a = strtod(numbers[0].c_str(), 0);
b = strtod(numbers[1].c_str(), 0);
printf("%f %f", a, b);
return 0;
}
Is there a better way to solve this problem, especially faster way due to reading thousands of lines like this?