LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-02-2005, 02:29 PM   #1
niteshadw
Member
 
Registered: Sep 2002
Distribution: CentOS
Posts: 170

Rep: Reputation: 15
adding to vector<string> in a struct


I'm trying to read in a file with chemical formulas and if there is another name with the same formula, I want to search through the vector struct and if it already exists, just add the name to the existing formula...however, I am having trouble adding the name of the formula to the struct because there is a vectors inside. Here is the struct and function that should read the opened file and fill the vector...

struct HydrocarbonData{
vector<string> name;
unsigned carbon;
unsigned hydrogen;
};


void fillVectorFromFile(vector<HydrocarbonData>& vecData, ifstream& inFile){
HydrocarbonData tempData;
char ch; //temporarily stores the element symbol
//ERRORS WITH THE NEXT LINE, works fine if the vector in the sturck is not there, only a string...
while(inFile >> tempData.name >> ch >> tempData.carbon >> ch >> tempData.hydrogen){

// check if there is an existing formula for a different name
for(size_t i = 0; i < vecData.size(); ++i){
if((tempData.carbon == vecData[i].carbon) && (tempData.hydrogen == vecData[i].hydrogen)){
//ERRORS WITH THE NEXT LINE
vecData.name[i].push_back(tempData.name); // add only name to existing formula
} //if
} //for

//vecData.push_back(tempData);
} //while
} //fillVectorFromFile

Any help would be appreciated, just how to add the name to vector<string> name.

This is what the file looks like:

n-Butane C4H10
Propyne C3H3
1,3-Butadiyne C4H2
Hexane C6H14
Butane C4H10
iso-Butane C4H10
Pentane C5H12

As you can see, there is a the same formula for iso-Butane and Butane...
 
Old 02-03-2005, 03:17 PM   #2
N43
LQ Newbie
 
Registered: Sep 2004
Location: Germany
Distribution: Slackware
Posts: 13

Rep: Reputation: 0
Quote:
vecData.name[i].push_back(tempData.name); // add only name to existing formula
You are accessing the content of the variable name. To add a new element you need to do it like this:
Code:
vecData.name.push_back(tempData.name); // add only name to existing formula
 
Old 02-03-2005, 04:59 PM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
You're using your HydrocarbonsData as the input variables for the inFile >> and you should probably use a set of temporary variables. Do a push_back with the string afterwards.

Instead of using structs, use classes in c++ leverage your logic in the classes.

The example below should have been split into about 5 files, but I'm a lazy ass.

Code:
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <ostream>

class HydrocarbonData 
{
  private:
    std::vector<std::string> name;
    unsigned  carbon;
    unsigned  hydrogen;

  public:
    unsigned getCarbon() { return carbon; } const
    unsigned getHydrogen() { return hydrogen; } const
    std::string getStrName() 
    { 
      if ( name.size() == 1 )
      {
         return ( name[0] );
      }
      else
      {
         std::cout << "Oh, shit!" << std::endl;
         exit(0);
      }
    }
    void setCarbon( const unsigned c ) { carbon = c; } 
    void setHydrogen( const unsigned h ) { hydrogen = h; } 
    void setName( const std::vector<std::string>& v ) { name = v; } 
    void setName( const std::string& n ) 
    {  
       bool iFound = false; 
       for ( int i = 0; i < name.size(); i++ )
       {
         if ( name[i] == n ) 
         {
           iFound = true; 
           break;
         }
       }
       if ( iFound == false )
       {
         name.push_back(n); 
       }
    }

    void dump()
    {
       std::cout << "HC: " << std::endl;
       for ( int i = 0; i <  name.size(); i++ )
       {
         std::cout << "    " << name[i] << std::endl;
       }
       std::cout << "     Carbon  : " << carbon << std::endl;
       std::cout << "     Hydrogen: " << hydrogen << std::endl;
    }
 
    friend std::ifstream & operator >> ( std::ifstream & iStream, HydrocarbonData & hd );

};

bool operator == (  HydrocarbonData& d1, HydrocarbonData& d2 )
{
   if ( d1.getCarbon() == d2.getCarbon() )
     if ( d1.getHydrogen() == d2.getHydrogen() )
        return true;
   return false;
}

std::ifstream & operator >> ( std::ifstream & iStream, HydrocarbonData & hd )
{  
   char ch;
   std::string strTmp;
   iStream  >> strTmp >> ch >> hd.carbon >> ch >> hd.hydrogen;
   hd.setName( strTmp );

   return ( iStream );
}

class Hydrocarbons
{
  private:  
    std::vector<HydrocarbonData>  vechc;

  public:
    void add( HydrocarbonData& hd )
    {
      bool iFound = false;
      for ( int i = 0; i < vechc.size(); i++ )
      {
        if ( hd == vechc[i] )
        {
          vechc[i].setName( hd.getStrName() ); 
          iFound = true;
          break;
        } // end if
      } // end for

      if ( iFound == false )
      {
        vechc.push_back( hd ); 
      }
    } 
   
    void dump()
    {
      for ( int i = 0; i < vechc.size(); i++ )
      {
        vechc[i].dump();
      } 
    }

    friend std::ifstream & operator >> ( std::ifstream & iStream, Hydrocarbons & hc );
};

std::ifstream & operator >> ( std::ifstream & iStream, Hydrocarbons & hc )
{
  HydrocarbonData hd;

  iStream >> hd;
  hc.add( hd );
   
  return( iStream ); 
}

int main()
{
  std::ifstream inFile;
  Hydrocarbons hc;

  inFile.open("data.txt" );

  while ( inFile >>  hc )
  {

  }

  hc.dump();

  return(0);
}
 
Old 02-03-2005, 07:08 PM   #4
niteshadw
Member
 
Registered: Sep 2002
Distribution: CentOS
Posts: 170

Original Poster
Rep: Reputation: 15
That's whole lot of code there, I really do appreciate it but it was something we were doing in an semi-into C++ lab. The lab workers have deep indian accents which are difficult to understand and when you ask for help when we're actually doing the coding, they talk about something completley irrelevant to the question and provide no help.

I do appreciate the help and I do understnad the code but they would notice that I was not 'taught' this thus even if I would know this, they would say someone else did it for me.

Well I'll try to figure out how to push into a vector while reading from a file in a smiple way...lol...once again I greatly apprecaite all the help =)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ adding an extra vector stops code from working timhardy Programming 4 08-17-2005 07:19 PM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM
Accessing a struct inside struct cxel91a Programming 1 09-17-2003 04:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration