Thread: RE: [Dev-C++] Question about structs...
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Marc H. <ma...@e-...> - 2002-10-23 07:49:10
|
Hi,
If you build an array of structs as you have below, you have a fixed number
of records. It might be a good idea to build your data structure (and I
suggest using a class which you can then use to validate data and perform
operations on your data) and then storing your "records" in a STL vector, or
similar. This allows you to dymaically change the number of employee, etc.
etc. etc. So you'd get something like:
A file called "employee.h", for example
_____________________________
#include <vector>
using namespace std;
class CEmployee
{
public:
CEmployee(); //constructor - init code
~CEmployee(); //destructor - clean up code
public:
BOOL SetId(int nId); //Get and set methods for props
int GetId(void);
BOOL SetAge(int nAge);
int GetAge(void);
BOOL SetSalary(float fSalary);
float GetSalary(void);
BOOL SetName(char *szName);
char* GetName(void);
private:
//private properties
int m_nId;
int m_nAge;
float m_fSalary;
char m_szName[25];
}
typedef vector<CEmployee*> EMPLOYEES_DATABASE;
_____________________________
And then use
_____________________________
#include <iostream>
#include "employee.h"
int main(void)
{
EMPLOYEES_DATABASE database;
CEmployee *employee = new CEmployee(); //Create new employee
employee->SetName("Marc Heiligers"); //Set employee properties
employee->SetAge(27);
employee->SetId(0);
employee->SetSalary(1000000.00);
database.push_back(employee); //Add employee to
end of vector
.
.
.
cout << "Number of employees: " << database.size() << endl;
//Use an iterator to loop through all employees and print their
names.
EMPLOYEES_DATABASE::iterator iter;
for (iter = database.begin(); iter != database.end; iter++)
{
employee = *iter;
cout << "Name: " << employee->GetName() << endl;
}
return 0;
}
_____________________________
So, to answer your question (umm, this may have been a bit of overkill),
yes, you can use employee_name[25] array. You would access each "record"
with a for loop. But there is no need to loop through the name array unless
you want to search the name for a particular character.
_____________________________
Marc Heiligers
-----Original Message-----
From: Rob Peacock [mailto:py...@ea...]
Sent: 22 October 2002 06:35 PM
To: dev...@li...
Subject: [Dev-C++] Question about structs...
OK, I'm back for more information. I'm looking at a slightly different way
of building the database I asked about yesterday.
The examples I have seen for a structure are typically a database. Here's
the one I have seen posted on several newbie sites:
#include <iostream.h>
struct database
{
int id_number;
int age;
float salary;
};
int main()
{
database employee;
employee.age=22;
employee.id_number=1;
employee.salary=120000.21;
return 0;
}
My question is this; this struct as it is written will only hold one record,
and all of the entries are int's. If I wanted to have multiple records using
the same struct, can I create a struct array (correct
terminology?) like this:
struct database[100]
{
int id_number;
char employee_name[25];
int age;
float salary;
};
To populate it, or to access the individual records within the array, I
would use the standard for() loops, correct? Same with the emplyee_name[]
array?
Thanks all,
--->Rob
|
|
From: Marc H. <ma...@e-...> - 2002-10-23 13:28:18
|
Hi Per W,
You're quite right about the GetXXX() methods. Early morning... Coding not
always best.
Ultimately , I would prefer to use a CString class for name and other string
type properties. Also use TCHAR instead of char, etc. etc. etc. CString
classes generally have a copy constructor so the result of GetName() would
not be able to change the value in the instance.
I did not know that about void. I knew it could be omitted in C++, but is it
convention to omit it?
As for the template container, I did say vector or similar. I guess it
depends on what features you need. Rob was talking about traversing the
"array" using for loops. I think a vector is quite handy for that. The point
was to get away from statically defined arrays.
_____________________________
Marc Heiligers
-----Original Message-----
From: Per Westermark [mailto:pw...@ia...]
Sent: 23 October 2002 02:55 PM
To: Marc Heiligers
Cc: 'Rob Peacock'; dev...@li...
Subject: RE: [Dev-C++] Question about structs...
What use is a Setname(char *szName) when the GetName() have already sent out
the pointer to the m_szName array? The GetName() method should be declared
const char* GetName() const; The First const to say that the returned
pointer may not be used to change the stored name. The second const to say
that the GetName() method doesn't change anything inside the class CEmployee
when called.
In fact, all GetXX() methods should be written as:
type GetXX() const;
Since this is C++, and not C, all methods not taking a parameter should be
written with empty parentheses. It is only in C, that the void keyword is
needed.
If using template containers, it might also be an idea to use the map
container instead, to allow direct sort and location of entries on customer
id.
/Per W
On Wed, 23 Oct 2002, Marc Heiligers wrote:
> Hi,
> If you build an array of structs as you have below, you have a fixed
> number of records. It might be a good idea to build your data
> structure (and I suggest using a class which you can then use to
> validate data and perform operations on your data) and then storing
> your "records" in a STL vector, or similar. This allows you to
> dymaically change the number of employee, etc. etc. etc. So you'd get
> something like:
>
> A file called "employee.h", for example _____________________________
> #include <vector>
>
> using namespace std;
>
> class CEmployee
> {
> public:
> CEmployee(); //constructor - init code
> ~CEmployee(); //destructor - clean up code
> public:
> BOOL SetId(int nId); //Get and set methods for props
> int GetId(void);
> BOOL SetAge(int nAge);
> int GetAge(void);
> BOOL SetSalary(float fSalary);
> float GetSalary(void);
> BOOL SetName(char *szName);
> char* GetName(void);
> private:
> //private properties
> int m_nId;
> int m_nAge;
> float m_fSalary;
> char m_szName[25];
> }
>
> typedef vector<CEmployee*> EMPLOYEES_DATABASE;
>
> _____________________________
> And then use
> _____________________________
> #include <iostream>
> #include "employee.h"
>
> int main(void)
> {
> EMPLOYEES_DATABASE database;
> CEmployee *employee = new CEmployee(); //Create new employee
> employee->SetName("Marc Heiligers"); //Set employee properties
> employee->SetAge(27);
> employee->SetId(0);
> employee->SetSalary(1000000.00);
> database.push_back(employee); //Add employee to
> end of vector
> .
> .
> .
> cout << "Number of employees: " << database.size() << endl;
>
> //Use an iterator to loop through all employees and print their
> names.
> EMPLOYEES_DATABASE::iterator iter;
> for (iter = database.begin(); iter != database.end; iter++)
> {
> employee = *iter;
> cout << "Name: " << employee->GetName() << endl;
> }
> return 0;
> }
> _____________________________
> So, to answer your question (umm, this may have been a bit of
> overkill), yes, you can use employee_name[25] array. You would access
> each "record" with a for loop. But there is no need to loop through
> the name array unless you want to search the name for a particular
> character.
>
> _____________________________
> Marc Heiligers
>
>
> -----Original Message-----
> From: Rob Peacock [mailto:py...@ea...]
> Sent: 22 October 2002 06:35 PM
> To: dev...@li...
> Subject: [Dev-C++] Question about structs...
>
>
> OK, I'm back for more information. I'm looking at a slightly different
> way of building the database I asked about yesterday.
>
> The examples I have seen for a structure are typically a database.
> Here's the one I have seen posted on several newbie sites:
>
> #include <iostream.h>
>
> struct database
> {
> int id_number;
> int age;
> float salary;
> };
>
> int main()
> {
> database employee;
> employee.age=22;
> employee.id_number=1;
> employee.salary=120000.21;
>
> return 0;
> }
>
> My question is this; this struct as it is written will only hold one
> record, and all of the entries are int's. If I wanted to have multiple
> records using the same struct, can I create a struct array (correct
> terminology?) like this:
>
> struct database[100]
> {
> int id_number;
> char employee_name[25];
> int age;
> float salary;
> };
>
> To populate it, or to access the individual records within the array,
> I would use the standard for() loops, correct? Same with the
> emplyee_name[] array?
>
> Thanks all,
>
> --->Rob
>
>
>
>
> -------------------------------------------------------
> This sf.net emial is sponsored by: Influence the future
> of Java(TM) technology. Join the Java Community
> Process(SM) (JCP(SM)) program now.
> http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0002en
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Per W. <pw...@ia...> - 2002-10-23 15:08:30
|
Well, Rob's original code snippet was strict C, so any use of templates
and classes might be outside the scope :-)
The problem with giving advice is always what conceptual level to use.
Template-based code with multiple sort orders normally requires a
smart-pointer implementation, which isn't trivial.
So in this case, a truly general application should probably store the
data in a MySQL database instead, but such a solution is clearly out of
scope of the original question...
About the void. Yes, in C++ it's considered unneccesary noise, best to
leave out of the code. The original use of func(void) was actually a
bit of a kludge to allow parametrized function prototypes in C, as opposed
to the original KR style.
/Per W
On Wed, 23 Oct 2002, Marc Heiligers wrote:
> Hi Per W,
>
> You're quite right about the GetXXX() methods. Early morning... Coding not
> always best.
>
> Ultimately , I would prefer to use a CString class for name and other string
> type properties. Also use TCHAR instead of char, etc. etc. etc. CString
> classes generally have a copy constructor so the result of GetName() would
> not be able to change the value in the instance.
>
> I did not know that about void. I knew it could be omitted in C++, but is it
> convention to omit it?
>
> As for the template container, I did say vector or similar. I guess it
> depends on what features you need. Rob was talking about traversing the
> "array" using for loops. I think a vector is quite handy for that. The point
> was to get away from statically defined arrays.
>
> _____________________________
> Marc Heiligers
>
>
> -----Original Message-----
> From: Per Westermark [mailto:pw...@ia...]
> Sent: 23 October 2002 02:55 PM
> To: Marc Heiligers
> Cc: 'Rob Peacock'; dev...@li...
> Subject: RE: [Dev-C++] Question about structs...
>
>
> What use is a Setname(char *szName) when the GetName() have already sent out
> the pointer to the m_szName array? The GetName() method should be declared
> const char* GetName() const; The First const to say that the returned
> pointer may not be used to change the stored name. The second const to say
> that the GetName() method doesn't change anything inside the class CEmployee
> when called.
>
> In fact, all GetXX() methods should be written as:
> type GetXX() const;
>
> Since this is C++, and not C, all methods not taking a parameter should be
> written with empty parentheses. It is only in C, that the void keyword is
> needed.
>
> If using template containers, it might also be an idea to use the map
> container instead, to allow direct sort and location of entries on customer
> id.
>
> /Per W
>
> On Wed, 23 Oct 2002, Marc Heiligers wrote:
>
> > Hi,
> > If you build an array of structs as you have below, you have a fixed
> > number of records. It might be a good idea to build your data
> > structure (and I suggest using a class which you can then use to
> > validate data and perform operations on your data) and then storing
> > your "records" in a STL vector, or similar. This allows you to
> > dymaically change the number of employee, etc. etc. etc. So you'd get
> > something like:
> >
> > A file called "employee.h", for example _____________________________
> > #include <vector>
> >
> > using namespace std;
> >
> > class CEmployee
> > {
> > public:
> > CEmployee(); //constructor - init code
> > ~CEmployee(); //destructor - clean up code
> > public:
> > BOOL SetId(int nId); //Get and set methods for props
> > int GetId(void);
> > BOOL SetAge(int nAge);
> > int GetAge(void);
> > BOOL SetSalary(float fSalary);
> > float GetSalary(void);
> > BOOL SetName(char *szName);
> > char* GetName(void);
> > private:
> > //private properties
> > int m_nId;
> > int m_nAge;
> > float m_fSalary;
> > char m_szName[25];
> > }
> >
> > typedef vector<CEmployee*> EMPLOYEES_DATABASE;
> >
> > _____________________________
> > And then use
> > _____________________________
> > #include <iostream>
> > #include "employee.h"
> >
> > int main(void)
> > {
> > EMPLOYEES_DATABASE database;
> > CEmployee *employee = new CEmployee(); //Create new employee
> > employee->SetName("Marc Heiligers"); //Set employee properties
> > employee->SetAge(27);
> > employee->SetId(0);
> > employee->SetSalary(1000000.00);
> > database.push_back(employee); //Add employee to
> > end of vector
> > .
> > .
> > .
> > cout << "Number of employees: " << database.size() << endl;
> >
> > //Use an iterator to loop through all employees and print their
> > names.
> > EMPLOYEES_DATABASE::iterator iter;
> > for (iter = database.begin(); iter != database.end; iter++)
> > {
> > employee = *iter;
> > cout << "Name: " << employee->GetName() << endl;
> > }
> > return 0;
> > }
> > _____________________________
> > So, to answer your question (umm, this may have been a bit of
> > overkill), yes, you can use employee_name[25] array. You would access
> > each "record" with a for loop. But there is no need to loop through
> > the name array unless you want to search the name for a particular
> > character.
> >
> > _____________________________
> > Marc Heiligers
> >
> >
> > -----Original Message-----
> > From: Rob Peacock [mailto:py...@ea...]
> > Sent: 22 October 2002 06:35 PM
> > To: dev...@li...
> > Subject: [Dev-C++] Question about structs...
> >
> >
> > OK, I'm back for more information. I'm looking at a slightly different
> > way of building the database I asked about yesterday.
> >
> > The examples I have seen for a structure are typically a database.
> > Here's the one I have seen posted on several newbie sites:
> >
> > #include <iostream.h>
> >
> > struct database
> > {
> > int id_number;
> > int age;
> > float salary;
> > };
> >
> > int main()
> > {
> > database employee;
> > employee.age=22;
> > employee.id_number=1;
> > employee.salary=120000.21;
> >
> > return 0;
> > }
> >
> > My question is this; this struct as it is written will only hold one
> > record, and all of the entries are int's. If I wanted to have multiple
> > records using the same struct, can I create a struct array (correct
> > terminology?) like this:
> >
> > struct database[100]
> > {
> > int id_number;
> > char employee_name[25];
> > int age;
> > float salary;
> > };
> >
> > To populate it, or to access the individual records within the array,
> > I would use the standard for() loops, correct? Same with the
> > emplyee_name[] array?
> >
> > Thanks all,
> >
> > --->Rob
> >
> >
> >
> >
> > -------------------------------------------------------
> > This sf.net emial is sponsored by: Influence the future
> > of Java(TM) technology. Join the Java Community
> > Process(SM) (JCP(SM)) program now.
> > http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0002en
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> >
>
|
|
From: Rob P. <py...@ea...> - 2002-10-23 18:31:00
|
At 05:07 PM 10/23/02 +0200, Per Westermark wrote: >Well, Rob's original code snippet was strict C, so any use of templates >and classes might be outside the scope :-) For this project, I would like to keep it as close to ANSI C as possible because I know more about C than C++ and I will be attempting to port it for use on a Mac as well. >Template-based code with multiple sort orders normally requires a >smart-pointer implementation, which isn't trivial. Since I am fairly new at this, I would like to keep it as simple as possible. Maybe after I get more experience I will improve on what I already have working. As it is, I already have a challenge ahead of me determining how to talk directly to my custom hardware via the serial port in NT. (Mac is easy by comparison - a couple of calls to the Pascal-based toolbox.) >So in this case, a truly general application should probably store the >data in a MySQL database instead, but such a solution is clearly out of >scope of the original question... If I knew how to interface my program with MySQL, I would certainly do it. Unfortunately, the only way I know how to interface with MySQL is via Apache for building web sites :) >/Per W >On Wed, 23 Oct 2002, Marc Heiligers wrote: >> Ultimately , I would prefer to use a CString class for name and other string >> type properties. Also use TCHAR instead of char, etc. etc. etc. CString >> classes generally have a copy constructor so the result of GetName() would >> not be able to change the value in the instance. Mark, I appreciate your detailed help. It is the attention to details, and the use of examples, that helps people learn and understand. Unfortunately, I am smart enough to realize that most everything you said went clear over my head :( Thank you very much for the assistance. Per, Once again you have given me something to think about with the MySQL. As my C instructor once said, there are almost as many ways of doing something as their are programmers and who is to say who has the 'most correct' way of solving the problem. Thanks again, --->Rob |
|
From: Louis G. <lg...@ya...> - 2002-10-23 19:03:56
|
Marc and Per: Thanks for the dialog from both of you. As a C++ newbie I learned new ideas about C++ database setup from your discussion of Rob's problem --- Rob Peacock <py...@ea...> wrote: > At 05:07 PM 10/23/02 +0200, Per Westermark wrote: > >Well, Rob's original code snippet was strict C, so any use of templates > >and classes might be outside the scope :-) > > For this project, I would like to keep it as close to ANSI C as possible > because I know more about C than C++ and I will be attempting to port it > for use on a Mac as well. > > >Template-based code with multiple sort orders normally requires a > >smart-pointer implementation, which isn't trivial. > > Since I am fairly new at this, I would like to keep it as simple as > possible. Maybe after I get more experience I will improve on what I > already have working. As it is, I already have a challenge ahead of me > determining how to talk directly to my custom hardware via the serial > port > in NT. (Mac is easy by comparison - a couple of calls to the > Pascal-based > toolbox.) > > >So in this case, a truly general application should probably store the > >data in a MySQL database instead, but such a solution is clearly out of > >scope of the original question... > > If I knew how to interface my program with MySQL, I would certainly do > it. > Unfortunately, the only way I know how to interface with MySQL is via > Apache for building web sites :) > > >/Per W > > >On Wed, 23 Oct 2002, Marc Heiligers wrote: > >> Ultimately , I would prefer to use a CString class for name and other > string > >> type properties. Also use TCHAR instead of char, etc. etc. etc. > CString > >> classes generally have a copy constructor so the result of GetName() > would > >> not be able to change the value in the instance. > > Mark, > > I appreciate your detailed help. It is the attention to details, and the > use of examples, that helps people learn and understand. Unfortunately, > I > am smart enough to realize that most everything you said went clear over > my > head :( > > Thank you very much for the assistance. > > Per, > > Once again you have given me something to think about with the MySQL. As > my > C instructor once said, there are almost as many ways of doing something > as > their are programmers and who is to say who has the 'most correct' way > of > solving the problem. > > Thanks again, > > --->Rob > > > ------------------------------------------------------- > This sf.net email is sponsored by: Influence the future > of Java(TM) technology. Join the Java Community > Process(SM) (JCP(SM)) program now. > http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0002en > > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users __________________________________________________ Do you Yahoo!? Y! Web Hosting - Let the expert host your web site http://webhosting.yahoo.com/ |
|
From: Per W. <pw...@ia...> - 2002-10-23 12:55:14
|
What use is a Setname(char *szName) when the GetName() have already sent
out the pointer to the m_szName array? The GetName() method should be
declared
const char* GetName() const;
The First const to say that the returned pointer may not be used to change
the stored name. The second const to say that the GetName() method doesn't
change anything inside the class CEmployee when called.
In fact, all GetXX() methods should be written as:
type GetXX() const;
Since this is C++, and not C, all methods not taking a parameter should be
written with empty parentheses. It is only in C, that the void keyword is
needed.
If using template containers, it might also be an idea to use the map
container instead, to allow direct sort and location of entries on
customer id.
/Per W
On Wed, 23 Oct 2002, Marc Heiligers wrote:
> Hi,
> If you build an array of structs as you have below, you have a fixed number
> of records. It might be a good idea to build your data structure (and I
> suggest using a class which you can then use to validate data and perform
> operations on your data) and then storing your "records" in a STL vector, or
> similar. This allows you to dymaically change the number of employee, etc.
> etc. etc. So you'd get something like:
>
> A file called "employee.h", for example
> _____________________________
> #include <vector>
>
> using namespace std;
>
> class CEmployee
> {
> public:
> CEmployee(); //constructor - init code
> ~CEmployee(); //destructor - clean up code
> public:
> BOOL SetId(int nId); //Get and set methods for props
> int GetId(void);
> BOOL SetAge(int nAge);
> int GetAge(void);
> BOOL SetSalary(float fSalary);
> float GetSalary(void);
> BOOL SetName(char *szName);
> char* GetName(void);
> private:
> //private properties
> int m_nId;
> int m_nAge;
> float m_fSalary;
> char m_szName[25];
> }
>
> typedef vector<CEmployee*> EMPLOYEES_DATABASE;
>
> _____________________________
> And then use
> _____________________________
> #include <iostream>
> #include "employee.h"
>
> int main(void)
> {
> EMPLOYEES_DATABASE database;
> CEmployee *employee = new CEmployee(); //Create new employee
> employee->SetName("Marc Heiligers"); //Set employee properties
> employee->SetAge(27);
> employee->SetId(0);
> employee->SetSalary(1000000.00);
> database.push_back(employee); //Add employee to
> end of vector
> .
> .
> .
> cout << "Number of employees: " << database.size() << endl;
>
> //Use an iterator to loop through all employees and print their
> names.
> EMPLOYEES_DATABASE::iterator iter;
> for (iter = database.begin(); iter != database.end; iter++)
> {
> employee = *iter;
> cout << "Name: " << employee->GetName() << endl;
> }
> return 0;
> }
> _____________________________
> So, to answer your question (umm, this may have been a bit of overkill),
> yes, you can use employee_name[25] array. You would access each "record"
> with a for loop. But there is no need to loop through the name array unless
> you want to search the name for a particular character.
>
> _____________________________
> Marc Heiligers
>
>
> -----Original Message-----
> From: Rob Peacock [mailto:py...@ea...]
> Sent: 22 October 2002 06:35 PM
> To: dev...@li...
> Subject: [Dev-C++] Question about structs...
>
>
> OK, I'm back for more information. I'm looking at a slightly different way
> of building the database I asked about yesterday.
>
> The examples I have seen for a structure are typically a database. Here's
> the one I have seen posted on several newbie sites:
>
> #include <iostream.h>
>
> struct database
> {
> int id_number;
> int age;
> float salary;
> };
>
> int main()
> {
> database employee;
> employee.age=22;
> employee.id_number=1;
> employee.salary=120000.21;
>
> return 0;
> }
>
> My question is this; this struct as it is written will only hold one record,
> and all of the entries are int's. If I wanted to have multiple records using
> the same struct, can I create a struct array (correct
> terminology?) like this:
>
> struct database[100]
> {
> int id_number;
> char employee_name[25];
> int age;
> float salary;
> };
>
> To populate it, or to access the individual records within the array, I
> would use the standard for() loops, correct? Same with the emplyee_name[]
> array?
>
> Thanks all,
>
> --->Rob
>
>
>
>
> -------------------------------------------------------
> This sf.net emial is sponsored by: Influence the future
> of Java(TM) technology. Join the Java Community
> Process(SM) (JCP(SM)) program now.
> http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0002en
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|