CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2016
    Posts
    1

    Question Need help with reading 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;

    HTML Code:
    ,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:


    Code:
    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();
       }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: Need help with reading a comma delimited data table

    Code:
    string contname;
           contList.push_back(contname);
    This will always push a null string to the vector as nothing has been assigned to contname from its definition to when it is pushed to the vector.

    Don't you mean
    Code:
    contList.push_back(field);
    Usually this would be coded as
    Code:
    while (getline(linestr, field, ',' ))
         contList.push_back(field);
    as getline() returns an istream.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured