I have a text file having 3 lines of arbitrary numbers like this:
3.50001
6.70001
812.333
I would want to input and save these 3 numbers into a vector in my programme. It should be read line by line, so i assume the vector size will be 3. But it come wit error that the line in text file is string rather than double. It cannot be saved into the vector. How can I turn these string into double? Thank you.
PS: the function std::stod
could not be resolved here properly due to Eclipse.
int main(int argc, char* argv[]) {
#include<vector>
#include <string>
string line;
std::vector<double> myvector(3);
ifstream myfile (argv[0]);
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
std::string::size_type sz;
cout << line << '\n';
double line_double = std::stod(line);
myvector.push_back(line_double);
}
myfile.close();