GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Nov-2009, 14:37
iblink iblink is offline
New Member
 
Join Date: Nov 2006
Posts: 23
iblink has a little shameless behaviour in the past
Question

Help finding number of entries in structure array


Hello. I have declared a structure array of a defined structure. Now i want to know how to find the number of entries i have in the array. The array is below.

CPP / C++ / C Code:
#define MAXEMPLOYEE 300

int main()
{
	struct  employeeInfo{
           int depCode;
           int empNr;
           char firstName[12];
           char lastName[12];
           int age;
	};
	struct employeeInfo  allEmployee[MAXEMPLOYEE];

I want to know the number of employee entries i have in the array allEmployee[] at any given time. Thank you in advance
Last edited by LuciWiz : 03-Nov-2009 at 14:42. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #2  
Old 03-Nov-2009, 15:06
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Help finding number of entries in strusture array


Well, the easy answer is 300.

But, what I'm guessing is that you want to be able to hold a maximum of 300 of them but initially zero. One way to do it is to declare a variable to hold the count. When you decide to add an employee, increment the count. Like:
CPP / C++ / C Code:
#define MAXEMPLOYEE 300

int main()
{
	struct  employeeInfo{
           int depCode;
           int empNr;
           char firstName[12];
           char lastName[12];
           int age;
	};

	struct employeeInfo  allEmployee[MAXEMPLOYEE];
	int employeeCount = 0;

	// Add the first employee
	employee[employeeCount].depCode = 1;
	employee[employeeCount].empNr = 1;
	strcpy(employee[employeeCount].firstName, "Eddie");
	strcpy(employee[employeeCount].lastName, "Haskell");
	employee[employeeCount].age = 65;
	++employeeCount;
  #3  
Old 03-Nov-2009, 15:48
iblink iblink is offline
New Member
 
Join Date: Nov 2006
Posts: 23
iblink has a little shameless behaviour in the past

Re: Help finding number of entries in strusture array


Assuming i use your method to add a certain number of employees. And i want to know now how many employees i have in the array allEmployee[]. How do i do that?
  #4  
Old 03-Nov-2009, 18:31
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Help finding number of entries in strusture array


Quote:
Originally Posted by iblink
Assuming i use your method to add a certain number of employees. And i want to know now how many employees i have in the array allEmployee[]. How do i do that?

Every time you add an employee, increment the count like we did in the example:
CPP / C++ / C Code:
++employeeCount;
If you want to know how many employees you have entered in, that number is now in employeeCount.

Maybe I am not understanding your question. Does this make sense?
  #5  
Old 03-Nov-2009, 23:15
iblink iblink is offline
New Member
 
Join Date: Nov 2006
Posts: 23
iblink has a little shameless behaviour in the past

Re: Help finding number of entries in strusture array


A bit embarrassed to say i didn't get what you meant until now.. Thanks a lot
  #6  
Old 04-Nov-2009, 10:49
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Help finding number of entries in strusture array


Quote:
Originally Posted by iblink
A bit embarrassed to say i didn't get what you meant until now.. Thanks a lot
No reason to get embarrassed. At one point I, too, did not have a good grasp of structures and arrays. Just keep practicing.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about finding numbers in a array are less than a certain number . allenfanwenyuan C++ Forum 5 10-May-2007 18:06
Converting a number amount to text Godzilla C++ Forum 5 31-Mar-2006 12:38
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 20:31
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 07:19
Using an array and finding the element number (subscript) tommy69 C Programming Language 27 05-Apr-2004 13:23

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:57.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.