ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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...
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
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);
}
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 =)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.