Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 26th, 2006, 03:15 AM
sd2004
Guest
 
Posts: n/a
Default Another STL ?

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$

  #2  
Old January 26th, 2006, 03:45 AM
I V
Guest
 
Posts: n/a
Default Re: Another STL ?

sd2004 wrote:[color=blue]
> ////////////////////// QUESTION //////////////////////////
> // how can I "store" value of variable "test" into member
> "number" of class "dog",[/color]

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.
[color=blue]
> // so that I can use iter->number to print out the data or other
> purposes.[/color]

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;
[color=blue]
> // What I have in mind is to use push_back as I did in line 40
> "v.push_back(dog)"[/color]

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:
[color=blue]
> int dog_info_class::dog_age (const int YearBorn){
> int age = (CurrentYear - YearBorn)*1;
> return age;
> }[/color]

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:
[color=blue]
> test=dog.dog_age(iter->YearBorn)*variable;[/color]

to:

test = iter->dog_age() * variable;

  #3  
Old January 26th, 2006, 04:25 AM
Larry I Smith
Guest
 
Posts: n/a
Default Re: Another STL ?

sd2004 wrote:

<snip>
Read and heed IV's post.

Also change this:
[color=blue]
> while (!getline(in,line).eof()){[/color]

to
while( getline( in, line ) ) {[color=blue]
> 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
> }[/color]
for some compilers, to be safe, you might add this line at the end
of the 'while':
line.erase();[color=blue]
> }[/color]
<snip>

Regards,
Larry
  #4  
Old January 26th, 2006, 05:25 AM
sd2004
Guest
 
Posts: n/a
Default Re: Another STL ?

Thanks all ("I V and "Larry Smith" ) for their help.

 

Bookmarks

Thread Tools

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles