473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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$

Jan 26 '06 #1
3 1554
I V
sd2004 wrote:
////////////////////// 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;

Jan 26 '06 #2
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
Jan 26 '06 #3
Thanks all ("I V and "Larry Smith" ) for their help.

Jan 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: luu duong | last post by:
I know this is probably easy but here is the details. I have an asp page that is not inside a frameset. I want to post data to another asp page that is inside a frameset. So firstpage.asp has...
6
by: anon | last post by:
Post Forwarding question...... For this control below, <asp:Button runat="server" PostTargetUrl="page2.aspx" /> The Attribute: PostTargetUrl="page2.aspx" Is this PostTargetUrl Attribute...
1
by: SC G | last post by:
Hi, I use a web application that has 120 checkboxes on it for me to select from before I submit a form. I have to select boxes in groups of 40 (1-40,41-80,81-120). Each submission generates a...
2
by: terence.parker | last post by:
I am often faced with the dilemma of whether to use a JOIN query across three tables in order to grab a bunch of results - or whether to create another table to represent what I want. The latter is...
5
by: Daniel Tan | last post by:
Are there anyway to copy rows of records from one query to another query and then hide the records in source query ? Pls advise. Thanks. Regards, Daniel
3
by: Kathy Burke | last post by:
Hi, I'm tired, so this question may be silly. I have a fairly long sub procedure. Based on one condition, I load another sub with the following: If Session("GRN") = "complete" Then txtScan.Text...
3
by: qwerty | last post by:
I´m new to ASP.Net. My workmate has some experience with it. He claimed that in ASP.Net working with frames is much simpler than it was ASP. I asked explanation but he couldn't give me such. (a...
17
by: Rabbit | last post by:
Hi, On my 1st page, i have a function which gets a new ID value and need to transfer to another immediately. which I want to get in 2nd page using Request.form("txtID"), but doesn't work, the...
16
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
3
by: Sin Jeong-hun | last post by:
It seems like the Protect() uses the Windows accout information to encrypt data. If I know the user name and the password, can I decrypt it on another PC? If it is not, how about the exported key?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.