Manipulating Data from CSV file
Apr 16, 2019 at 3:52pm UTC
I have the following code below, and was wondering what was the next step I could take to calculate the percent change of the open and close data from the file? I need to display percent change of the data from highest to lowest and I can't figure out how I would access each row individually to do so.
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include <limits>
using namespace std;
struct Stock
{
std::string date;
double open;
double high;
double low;
double close;
double adjClose;
double volume;
};
int main()
{
ifstream myFile("AAPL.csv" );
if (!myFile)
{
cout << "ERROR: File wasn't opened." << "\n" ;
return 1;
}
vector<Stock> Data;
Stock temp;
char comma{};
while (getline(myFile, temp.date, ',' ))
{
myFile >> temp.open; myFile >> comma;
myFile >> temp.high; myFile >> comma;
myFile >> temp.low; myFile >> comma;
myFile >> temp.close; myFile >> comma;
myFile >> temp.adjClose; myFile >> comma;
myFile >> temp.volume;
myFile.ignore(numeric_limits<streamsize>::max(), '\n' ); //Clears input buffer to make way for next getline
Data.emplace_back(temp);
}
system("pause" );
return 0;
}
Apr 16, 2019 at 5:52pm UTC
Well you stored the information in a vector, do you know how to access that vector?
By the way do you know you can chain the input operations?
1 2 3 4 5 6 7 8 9 10
while (getline(myFile, temp.date, ',' ))
{
myFile >> temp.open >> comma
>> temp.high >> comma
>> temp.low >> comma
>> temp.close >> comma
>> temp.adjClose >> comma
>> temp.volume;
myFile.ignore(numeric_limits<streamsize>::max(), '\n' ); //Clears input buffer to make way for next getline
Last edited on Apr 16, 2019 at 7:35pm UTC
Topic archived. No new replies allowed.