converting part of a vector from string to double/int
Hey everyone.
I'm having a bit of trouble with a programme that I'm writing which requires input from a csv file, the application of an algorithm (the solar position algorithm or spa) using input from each row consecutivey and then outputting the results as another csv file.
I have no problem applying the algorithm to one line, or with inputting and outputting the csv, but I'm having a lot of difficulty knowing how best to write the loop so that it assigns certain columns to elements of my structure when working down the rows of the csv file.
The main problem seems to be converting from an input string value to an int or double which is required as arguments for the functions of the algorithm.
I tried using atoi, but either I don't know how to use it properly or it isn't the best option for what I'm trying to do.
Any help or advice on this would be a huge help with the work I'm doing...
Thanks in advance
Jake
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdio>
#include "spa.h" //include the SPA header file
#include "spa.c"
using namespace std;
spa_data spa;
void csvline_populate(vector<string> &record, const string& line, char delimiter);
int main()
{
vector<string> row;
string line;
ifstream in("radiation_budget_data.csv"); //Just the name of the .csv file I'm using
if (in.fail()) { cout << "File not found" <<endl; return 0; }
while(getline(in, line) && in.good() )
{
csvline_populate(row, line, ',');
for(int i=0, leng=row.size(); i<leng; i++)
spa_data spa; //declare the SPA structure - found in the header file spa.h attached
int result;
float min, sec;
spa.year = atoi(row[2]);
spa.month = atoi (row[3]);
spa.day = atoi (row[4]);
spa.hour = 12;
spa.minute = 00;
spa.second = 00;
spa.timezone = +1.0;
spa.delta_t = 67;
spa.longitude = atoi(row[8]);
spa.latitude = atoi(row[7]);
spa.elevation = atoi(row[9]);
spa.pressure = 820;
spa.temperature = 30;
spa.slope = atoi(row[6]);
spa.azm_rotation = 0;
spa.atmos_refract = 0.5667;
spa.atmos_trans = 0.67; //Gates (1980) - between 0.6 and 0.7/////////////
spa.ext_flux_d = 1200;
spa.function = SPA_ALL;
cout << spa.day << ",";
cout << endl;
}
in.close();
cin.get();
return 0;
}
void csvline_populate(vector<string> &record, const string& line, char delimiter)
{
int linepos=0;
int inquotes=false;
double c;
int i;
int linemax=line.length();
string curstring;
record.clear();
while(line[linepos]!=0 && linepos < linemax)
{
c = line[linepos];
if (!inquotes && curstring.length()==0 && c=='"')
{
//begin quotechar
inquotes=true;
}
else if (inquotes && c=='"')
{
//quotechar
if ( (linepos+1 <linemax) && (line[linepos+1]=='"') )
{
//encountered 2 double quotes in a row (resolves to 1 double quote)
curstring.push_back(c);
linepos++;
}
else
{
//endquotechar
inquotes=false;
}
}
else if (!inquotes && c==delimiter)
{
//end of field
record.push_back( curstring );
curstring="";
}
else if (!inquotes && (c=='\r' || c=='\n') )
{
record.push_back( curstring );
return;
}
else
{
curstring.push_back(c);
}
linepos++;
}
record.push_back( curstring );
return;
}
ps I've included the header and spa.c as attachments as well as the csv file as a .txt