converting part of a vector from string to double/int

This is a discussion on converting part of a vector from string to double/int within the C++ Programming forums, part of the General Programming Boards category; Hey everyone. I'm having a bit of trouble with a programme that I'm writing which requires input from a csv ...

  1. #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 Attached Files

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    #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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Dec 2009
    Location
    Cape Town, South Africa
    Posts
    8
    Thanks a lot, seems to be working now!

  4. #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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

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