help with "aborted (core dumped error)" when trying to convert strings to doubles

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)
Don't use .eof to test the end of the file. You won't reach that point until you have tried and failed to read any more data. Test the condition of the stream after the first getline statement for a line. You can put a getline call inside the while( ) test.

At a rough guess you have tried to convert with stod() the latter part of the last line, that being the last thing read successfully by getline.
Last edited on
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

int main ()
{
    std::string line;
    std::ifstream myfile ("dis.txt");
    
    std::stringstream iss;
    
    if (myfile.is_open())
    {
        std::getline(myfile, line);
        std::cout << "First line ignored:\n" << line << '\n';
        
        double number = 0;
        std::string dummy;
        char comma;
        
        while (myfile >> line)
        {
            iss << line;
            std::cout << "\n*** " << iss.str() << " ***\n";
            
            std::getline(iss, dummy, ',');
            
            while
                (
                 iss
                 >> number >> comma
                 >> number >> comma
                 >> number >> comma
                 >> number >> comma
                 )
            {
                std::cout << number << '\n';
                
                iss.str( std::string() );
                iss.clear();
            }
            
            iss.str( std::string() );
            iss.clear();
            
        }
        myfile.close();
    }
    
    else std::cout << "Unable to open file";
    
    return 0;
}


First line ignored:
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 ***
99.36

*** 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 ***
100.65

*** 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 ***
98.54

*** 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 ***
100.6
Program ended with exit code: 0
If you have UNIX tools available, just extract the 5th column from the command line:
cut -d, -f5 < dis.txt
Topic archived. No new replies allowed.