I have a .txt file where I have to extract just the fifth column and save it in a vector as doubles. This is a sample of the data:
Date,Open,High,Low,Close,Volume,Ex-Dividend,Split Ratio,Adj. Open,Adj. High,Adj. Low,Adj. Close,Adj. Volume
2018-03-27,100.88,101.18,98.89,99.36,7131436.0,0.0,1.0,100.88,101.18,98.89,99.36,7131436.0
2018-03-26,99.86,100.78,99.0782,100.65,7262326.0,0.0,1.0,99.86,100.78,99.0782,100.65,7262326.0
2018-03-23,100.85,101.11,98.45,98.54,7380554.0,0.0,1.0,100.85,101.11,98.45,98.54,7380554.0
2018-03-22,101.29,101.64,100.41,100.6,8648198.0,0.0,1.0,101.29,101.64,100.41,100.6,8648198.0
The entire data goes all the way back to 1962, it is stock info that I got using quandl API, I thought there was a way to just extract a specific column from quandl but for some reason it wasn't working so now I am here trying to read the file and convert those closing prices to doubles and save them into a vector.
This is my code and this is the error I get (this was the first thing I thought of, if anyone knows any better ways to get just a certain column I'm open to suggestions)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
int main(){
string STRING;
double closingvalue;
vector<double> close;
ifstream infile;
infile.open ("DIS.txt");
getline(infile,STRING); // Saves the line in STRING.
cout<<STRING <<endl; // Prints first line
while(!infile.eof()){
for(int i=1; i<=5; ++i){
getline(infile,STRING, ',');
if(i%5==0){
closingvalue = stod(STRING);
cout<<closingvalue <<" ";
close.push_back(closingvalue);
}
}
getline(infile, STRING);
}
infile.close();
for(int i=0; i<close.size(); ++i){
cout<<close[i] <<" ";
}
return 0;
}
| |
the compiler prints out all of the closing values because of the line
cout<<closingvalue <<" ";
so I know it's doing up to that part correct, but then it prints this and doesn't do anything after the for loop even when trying to print the vector.
1 2 3
|
terminate called after throwing an instance of 'std::invalid_argument'
what(): stod
Aborted (core dumped)
| |