C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-09-2009, 08:26 AM   #1
Registered User
 
Join Date: Dec 2009
Location: Cape Town, South Africa
Posts: 8
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
Attached Files
File Type: h spa.h (9.8 KB, 23 views)
File Type: c spa.c (38.2 KB, 25 views)
File Type: txt radiation_budget_data.txt (179.0 KB, 21 views)
jakethehake is offline   Reply With Quote
Old 12-09-2009, 08:54 AM   #2
Registered User
 
hk_mp5kpdw's Avatar
 
Join Date: Jan 2002
Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 3,030
#1.
Code:
#include "spa.c"
You shouldn't be including source files. Other source files should be added as a part of your project. This is done either through settings in your IDE or additional parameters in a makefile or command line compiler.

#2.
Code:
while(getline(in, line)  && in.good() )
A nit, but that last part in red isn't needed.

#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;  

...
Is that a typo? Without brackets ({}), the above for loop will only loop over the single statement below it (the declaration of your spa variable). If you want to loop over more than one statement, then you need those brackets. Based on the remaining code, I wonder what you need the for loop for.

#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]);
If you look at the available documentation for atoi, you'd see that it accepts a const char* argument but you are attempting to pass in string objects. If you're going to use atoi you therefore need to pass in the correct argument type. Thankfully, string objects have a c_str method that returns what you need, for example:
Code:
spa.year          = atoi(row[2].c_str());                                                     
spa.month         = atoi (row[3].c_str());
#5.
Code:
int inquotes=false;
Should probably be a bool type instead of int.
__________________
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   Reply With Quote
Old 12-10-2009, 04:04 AM   #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   Reply With Quote
Old 12-10-2009, 04:19 AM   #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());
Not quite sure, because it still gives me a warning that says:

[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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:11 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22