![]() |
| | #1 |
| Registered User Join Date: Dec 2009 Location: Cape Town, South Africa
Posts: 8
| converting part of a vector from string to double/int 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;
}
|
| jakethehake is offline | |
| | #2 |
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 3,030
| #1. Code: #include "spa.c" #2. Code: while(getline(in, line) && in.good() ) #3. Code: ...
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;
...
#4. Code: 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]); Code: spa.year = atoi(row[2].c_str()); spa.month = atoi (row[3].c_str()); Code: int inquotes=false;
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 There are no stupid questions, only stupid people. 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 |
| hk_mp5kpdw is offline | |
| | #3 |
| Registered User Join Date: Dec 2009 Location: Cape Town, South Africa
Posts: 8
| Thanks a lot, seems to be working now! |
| jakethehake is offline | |
| | #4 |
| Registered User Join Date: Dec 2009 Location: Cape Town, South Africa
Posts: 8
| Just one more question, if I wanted to convert the string to a double would I simply write something along the lines of Code: spa.latitude = atof (row[7].c_str()); spa.elevation = atof (row[9].c_str()); [Warning] passing `double' for converting 1 of `void std::basic_string<_CharT, _Traits, _Alloc>: ush_back(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'Thanks a lot for your previous help, really made things a lot easier |
| jakethehake is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C++ ini file reader problems | guitarist809 | C++ Programming | 7 | 09-04-2008 06:02 AM |
| We Got _DEBUG Errors | Tonto | Windows Programming | 5 | 12-22-2006 05:45 PM |
| Message class ** Need help befor 12am tonight** | TransformedBG | C++ Programming | 1 | 11-29-2006 11:03 PM |
| can anyone see anything wrong with this code | occ0708 | C++ Programming | 6 | 12-07-2004 12:47 PM |
| Classes inheretance problem... | NANO | C++ Programming | 12 | 12-09-2002 03:23 PM |