Another STL ?

S

sd2004

Please help /explain my question embeded in the source code below.
Thanks in advance for your time,

////////////////////// source code ///////////////////////////////
#include<iostream>
#include <string>
#include<vector>
#include<sstream>
#include<fstream>
#include<numeric>
using namespace std;

class dog_info_class
{
public:
string name;
int id;
int YearBorn;
string type;
int dog_age(const int year);
int number;
};


#define goldenretriever 11
#define Lab 33
#define Boxer 44
#define Terrier 55
int const CurrentYear =2006;

int main()
{
vector<dog_info_class> v;

dog_info_class dog;

ifstream in ("test5g.txt");
string line;
while (!getline(in,line).eof()){
if (line.find("#")!=std::string::npos)continue;
istringstream is(line);
if(is>>dog.name>>dog.id>>dog.YearBorn>>dog.type)
{
v.push_back(dog); // line 40
}
}
vector<dog_info_class>::iterator iter;
for (iter=v.begin();iter!=v.end();++iter){
int variable = 2;
int results;
int test;
switch (iter->id){
case goldenretriever:
////////////////////// QUESTION //////////////////////////
// how can I "store" value of variable "test" into member
"number" of class "dog",
// so that I can use iter->number to print out the data or other
purposes.
// What I have in mind is to use push_back as I did in line 40
"v.push_back(dog)"
// Is that possible ?

test=dog.dog_age(iter->YearBorn)*variable;

cout << "number IS " << test <<endl;
cout << "number IS " << iter->number <<endl; // Not working

break;
case Lab:
test=dog.dog_age(iter->YearBorn)*variable;
results=dog.dog_age(iter->YearBorn);
break;
default:
break;
}

}
vector<dog_info_class>::iterator Iter;
for (Iter=v.begin();Iter!=v.end();++Iter){
// do something
}

return 0;

}
int dog_info_class::dog_age (const int YearBorn){
int age = (CurrentYear - YearBorn)*1;
return age;
}

///////////////////// input file "test5g.txt'''
/////////////////////////

########################################
#Name ID YearBorn Type
########################################
Cricket 11 2000 GoldenRetriever
Nitro 11 2005 GoldenRetriever
Maxtor 33 2004 Lab
Arron 44 2001 Boxer
#Arron 54 2002 Boxer
Dora 55 2000 Terrier


//////////////////////// OUTPUT ///////////////////////////

bash-2.05b$ ./a
number IS 12
number IS 1628391360
number IS 2
number IS 1628391360
bash-2.05b$
 
I

I V

sd2004 said:
////////////////////// QUESTION //////////////////////////
// how can I "store" value of variable "test" into member
"number" of class "dog",

Just to be clear on the terminology here - 'dog' is an object, which is
of class 'dog_info_class'.

However, you also have other objects of class dog_info_class, which are
stored in the vector. Note that each of these objects is separate from
the object called 'dog'; when you call v.push_back(dog), that adds a
_copy_ of the object called 'dog' into the vector.
// so that I can use iter->number to print out the data or other
purposes.

If you want to do anything to the objects in the vector, you must
change these objects, not the object called 'dog'. So, for instance,
you could set the value of 'number' for the specific object referred to
by 'iter' by doing:

iter->number = test;
// What I have in mind is to use push_back as I did in line 40
"v.push_back(dog)"

If you do this, it will add another object to the vector. Is that what
you want to do?

Also, I have a question about this function:
int dog_info_class::dog_age (const int YearBorn){
int age = (CurrentYear - YearBorn)*1;
return age;
}

Why are you passing it a value for 'YearBorn'? Each dog_info_class
object already has a value for YearBorn. So you can use that value:

int dog_info_class::dog_age()
{
return CurrentYear - YearBorn;
}

Then, in your loop, change:
test=dog.dog_age(iter->YearBorn)*variable;

to:

test = iter->dog_age() * variable;
 
L

Larry I Smith

sd2004 wrote:

<snip>
Read and heed IV's post.

Also change this:
while (!getline(in,line).eof()){

to
while( getline( in, line ) ) {
if (line.find("#")!=std::string::npos)continue;
istringstream is(line);
if(is>>dog.name>>dog.id>>dog.YearBorn>>dog.type)
{
v.push_back(dog); // line 40
}
for some compilers, to be safe, you might add this line at the end
of the 'while':
line.erase();
<snip>

Regards,
Larry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top