Read a comma delimited data table

I have been trying to read following data table and create an object for the HUBs(rows) and another object for continent (columns). Since I am not a C++ experienced user I have been facing some difficulties. The data is in following. The number after HUB and the dash shows the order from the hub. The other numbers under each continent are the corresponding cost and tariffs between a HUB and continent. I would like to be able to cout for instance following and get the result which would be 73. cout << hub(1)->cont(USA)->transport() << endl;

,USA,EUROPE,ASIA
HUB1-12000,,,
Transportation Cost,73,129,141
Tariffs,5,5,1
ShippingType,a,b,c
OtherFees,0.6,0.3,0.8
HUB2-11000,,,
Transportation Cost,57,101,57
Tariffs,7,7,5
ShippingType,b,b,d
OtherFees,0.7,0.3,0.6

Really appreciate your help. Here is what I have tried so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
void Hub()
   {
     string file = "/hubs.csv";         

     // 1-First read the first line and save the continent name
     string str, field;
     getline( fin, str );
     vector<string> contList;
     stringstream linestr( str );
     while (  linestr.good() )
     {
       getline( linestr, field, ',' );
       string contname;
       contList.push_back(contname);
     }

     // 2-Then read the rest
     getline( fin, str );
     while ( !fin.eof() )  // Read the whole file
     {
       stringstream linestr( str );
       string contname, order;
       if ( qstr[0] == 'HUB1' || qstr[0] == 'HUB2')         
       {
         // Read the name of the hub
         getline( linestr, hubname, ',' );           // Read the hub name
         getline( linestr, order, ',' );             // Read the order quantityity

         int quantity;
         istringstream orderstream( order);
         orderstream >> quantity;

         // Find the hub and add the order to the hub
         Hub* hub = glob->FindHubName( hubname ); // this returns a pointer 
         if ( glob->FindHubName( hubname ) == nullptr )
         {
           hubNotFound.push_back( hubname );
           getline( fin, qstr );
           continue;
         }
         hub->TotalOrder( quantity );
       }
       else if ( qstr[0] != 'HUB1' || qstr[0] != 'HUB2')   
       {
         // Read costs and tariffs 
         cout << hub(1)->cont(ASIA)->transport() 
        }
       getline( fin, qstr );
     }
     fin.close();
   }
Last edited on
If you are familiar with regular expressions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <regex>
#include <map>
#include <iomanip>

std::vector<std::string> get_continents( const std::string& line )
{
   static const std::regex words( "\\w+" ) ;

   // http://en.cppreference.com/w/cpp/regex/regex_token_iterator
   return { std::sregex_token_iterator( line.begin(), line.end(), words ), std::sregex_token_iterator{} } ;
}

int get_hub( const std::string& line )
{
   static const std::regex hub( "^HUB(\\d+)\\-.*" ) ;
   std::smatch match ;

   // http://en.cppreference.com/w/cpp/regex/regex_search
   if( std::regex_search( line, match, hub ) ) return std::stoi( match[1] ) ;
   else return -1 ; // no match
}

std::pair< std::string, std::vector<int> > get_name_and_values( const std::string& line )
{
    std::pair< std::string, std::vector<int> > name_and_values ;

    // get the name
    std::istringstream stm(line) ;
    std::getline( stm, name_and_values.first, ',' ) ;

    // get the values from the remainder of the line
    const std::string residue = stm.str() ;
    static const std::regex values( "\\d+" ) ;
    const std::sregex_token_iterator end{} ;
    for( std::sregex_token_iterator iter( residue.begin(), residue.end(), values ) ; iter != end ; ++iter )
        name_and_values.second.push_back( std::stoi(*iter) ) ;

    return name_and_values ;
}

int main()
{
    std::cout << "continents: [ " ;
    for( auto cont : get_continents( ",USA,EUROPE,ASIA" ) ) std::cout << std::quoted(cont) << ' '  ;
    std::cout << "]\n" ;

    std::cout << "hub: " << get_hub( "HUB23-12000,,," ) << '\n' ;

    const auto name_and_values = get_name_and_values( "Transportation Cost,73,129,141" ) ;
    std::cout << "name: '" << name_and_values.first << "'  values: [ " ;
    for( int v : name_and_values.second ) std::cout << v << ' ' ;
    std::cout << "]\n" ;
}

http://coliru.stacked-crooked.com/a/89ba6d53ef57804e
http://rextester.com/HYVK9658

Take it up from there.
Topic archived. No new replies allowed.