Having trouble getting data from my .csv file to vectors. When I open the .csv file in notepad it looks like this:
80.06,16.16,3.62
78.08,24.26,2.96
82.94,22.1,2.53
87.08,46.04,5.57
87.98,53.96,16.96
96.98,59,3.95
104,73.04,0.92
105.98,64.94,0.46
100.94,64.04,2.14
96.08,51.08,9.82
82.94,30.2,9.86
82.04,31.1,3.83
This is my code:
#include<iostream>
#include<vector>
#include<fstream>
#include<string>
using namespace std;
void populateData(vector<double>&, vector<double>&, vector<double>&);
int main() {
int monthNumber;
vector<double> tempHigh;
vector<double> tempLow;
vector<double> pNumbers;
tempHigh.reserve(12);
tempLow.reserve(12);
pNumbers.reserve(12);
populateData(tempHigh, tempLow, pNumbers);
for (auto& high : tempHigh) {
cout << high << endl;
}
for (auto& low : tempLow) {
cout << low << endl;
}
for (auto& pre : pNumbers) {
cout << pre << endl;
}
return 0;
}
void populateData(vector<double>& high, vector<double>& low, vector<double>& pre) {
ifstream inFile;
string token;
double weatherHigh;
double weatherLow;
double weatherPre;
inFile.open("2015MonthlySummary.csv");
if (!inFile.is_open()) {
cout << "Error opening file\n";
exit(12);
}
while (!inFile.eof()) {
getline(inFile, token, ',');
weatherHigh = stod(token);
high.push_back(weatherHigh);
getline(inFile, token, ',');
weatherLow = stod(token);
low.push_back(weatherLow);
getline(inFile, token, '\n');
weatherPre = stod(token);
pre.push_back(weatherPre);
}
inFile.close();
}
Having trouble getting data from my .csv file to vectors. |
You didn't state what the trouble was?
... though I can guess it is related to
while (!inFile.eof())
which is not recommended.
Last edited on