This is the code I'm using to attempt to read two columns of data from a csv file into a vector in C++. I'm able to read in the headers, but when i continue into reading the actual data I'm getting an error involing 'stof', which converts string to float. I'm pretty sure that this error results from stof not being able to find any amount of string to convert to a float, but this makes no sense as the variable inside the parenthesis is indeed a string, that is holding digits.
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
ifstream inFS;
string line;
string rm_in, medv_in;
const int MAX_LEN = 1000;
vector<double> rm(MAX_LEN);
vector<double> medv(MAX_LEN);
cout << "Opening file MyData.csv" << endl;
inFS.open("MyData.csv");
if(!inFS.is_open())
{
cout << "Could not open file MyData.csv" << endl;
return 1;
}
cout << "Reading line 1" << endl;
getline(inFS, line);
cout << "heading :" << line << endl;
int numObservations = 0;
while(inFS.good())
{
getline(inFS, rm_in , ',');
getline(inFS, medv_in , '\n');
rm.at(numObservations) = stof(rm_in);
medv.at(numObservations) = stof(medv_in);
numObservations++;
}
rm.resize(numObservations);
medv.resize(numObservations);
}
The error I'm getting is this: error