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
|