Ok, I am trying to figure out how to purse this current project which deals with dynamic allocation and linked list

Here is the information I am given the following two structs

struct employee
{
  int ssn;
  string name;
  float hours;
  assignment * list;
};

struct assignment
{
  string proj;
  float hours;
  assignment *next;
};

Objectives:

read from a file into a dynamic array of employee struct, size of array will be 3 when program starts, int ssn will serve as unique identifer.

read the second file and add a new node to the employee's linked list of assigned projects. The final arrangement
will be an array (dynamically allocated) of struct1 where each employee contains a pointer to a linked
list of the projects to which they are assigned. The employees in the array will be in ascending order by
SSN. The projects assigned to each employee will be in descending order by the hours assigned.

Any tips to get me going?

Yes, start out by opening the file and learning how to just read it. Just display the file contents on the screen.

After you get that going, you should be able to start reading the file contents into the structure. You didn't say how the file is layed out so I can't really help you any more.

I am not a first time posted here dragon, obviously.
However, I dont expect you to remember my skill set.

I know how to open a file, read from a file, read into a struct array, class array etc. Ive never used a dynamic array which shouldnt be too hard, ive never worked with linked lists which are challenging to me right now.

The first file looks something like

1233434 Name 40
4444445 Name 23

First number is the SSN
Next is the name
then hours.

Bump, I know there are some experienced C++ users in this forum, all help is appreciated.

I am not a first time posted here dragon, obviously.
However, I dont expect you to remember my skill set.

I know how to open a file, read from a file, read into a struct array, class array etc. Ive never used a dynamic array which shouldnt be too hard, ive never worked with linked lists which are challenging to me right now.

The first file looks something like

1233434 Name 40
4444445 Name 23

First number is the SSN
Next is the name
then hours.

You won't be doing any dynamic re-allocation because the original size is 3 and you have two records.

You need to figure out what factor you want to resize the dynamic array by. A decent default is to double it each time you resize it larger and halve it every time you make it smaller. To do so, allocate the new memory for the appropriate size, do a deep copy of the old array to the new array, then delete the old array. That's if you want to use "new" and "delete". You can also use "realloc". It's more efficient due to the fact that often no deep copying needs to be done.

I'd drop the assignment for now and practice dynamically allocating and resizing a plain old integer array and populating a plain old integer linked list, since these are both new concepts to you. Learn them, then come back and make it work with the structs. Then try to integrate the two into your assignment.

Well I believe the array gets resized depending on the number of items in the list each line read is one item so I am going to problem read the file for first for a count of elements something like this

fin.open("some.file")

while(fin>>ssn>>name>>hours)
{
 n++
}

and where n in the count of items in the array since the output will be formatted this way

and doing something like

employee emparr = new emparr[n]

I know thats not the exact syntax but you get my idea, thanks for the tips, ive played about a little with linked lists due to my time constraints ill report back later once ive played around with both which with my last assignment playing around with code around the web and book, helped when learning templates and overloading functions

read from a file into a dynamic array of employee struct, size of array will be 3 when program starts, int ssn will serve as unique identifer.

I'm focusing in on the "size of array will be 3 when program starts" part of your first post.

Well I believe the array gets resized depending on the number of items in the list each line read is one item so I am going to problem read the file for first for a count of elements something like this

fin.open("some.file")

while(fin>>ssn>>name>>hours)
{
 n++
}

and where n in the count of items in the array since the output will be formatted this way

and doing something like

employee emparr = new emparr[n]

This doesn't match the description as I read it. I read it like this, assuming there are 14 elements in the file:

  1. Start with array size 3.
  2. Read in first three elements.
  3. Before reading in 4th element, resize array to 6.
  4. Read in elements 4 to 6.
  5. Before reading in element 7, resize array to size 12.
  6. Read in elements 7 through 12.
  7. Before reading in element 13, resize the array to 24.
  8. Read in elements 13 and 14.

Again, that's my interpretation and I'm having to fill in some blanks. There's no guarantee that my interpretation is correct, but I see no other reason for the initial size to be 3 when the program starts. You're basically mimicking the behavior of a vector. You should ask your teacher to clarify.

Well the first three elements are considered one item in the array

emparr[0].ssn
emparr[1].name
emparr[0].hours

He really didnt clarify all he said default size of 3

Well the first three elements are considered one item in the array

emparr[0].ssn
emparr[1].name
emparr[0].hours

He really didnt clarify all he said default size of 3

Yeah, I'm treating the ssn, name, and hours as one object, so I'm thinking a 14 line file, each line containing those three items.

You'll either need clarification or you'll need to make some assumptions. If you have a 14 line file and the default size is three, what are you going to do when you try to store something in index 3? There's no room, so you can't read it in without resizing. Again, that's how I read it, but it's open to interpretation, so if you're reading it differently, you should ask for clarification on what to do with a 14 element input file. You really can't move on till you know.

Then again I guess that would be the purpose of this assignment is to learn dynamic allocation, so if I set the default to 3 and if comes to some n element greater than three I suppose this would work

while(infile)
{
  if(n >= 3)
   emparr = new emparr[n];
}

I am going go with the purpose of dynamic allocation and say the array will need resizing but I believe the code above isnt entirely correct because Ill be declaring a new emparr which i will have to delete the first array before hand and before the delete ill had to copy the array delete the current one used for reading and then declare a new array copy old array into new and delete old I assume, that seems a tad arguious because i dont believe he use and functions like memcpy or something of the sort

Then again I guess that would be the purpose of this assignment is to learn dynamic allocation, so if I set the default to 3 and if comes to some n element greater than three I suppose this would work

while(infile)
{
  if(n >= 3)
   emparr = new emparr[n];
}

I am going go with the purpose of dynamic allocation and say the array will need resizing but I believe the code above isnt entirely correct because Ill be declaring a new emparr which i will have to delete the first array before hand and before the delete ill had to copy the array delete the current one used for reading and then declare a new array copy old array into new and delete old I assume, that seems a tad arguious because i dont believe he use and functions like memcpy or something of the sort

What's wrong with memcpy? If it's useful, use it. If not, don't. Resizing inevitably involves deep copies sometimes (sometimes not, depends on how you do it). Whenever you deep copy with a reallocation, you have to free the old memory. So if you have a "new" without a corresponding "delete" somewhere that's bad. If you don't want to deal with deep copying, you may want to read up on realloc, but if you're not used to malloc, calloc, and realloc, you may want to stick with "new" and "delete".

http://www.cplusplus.com/reference/clibrary/cstdlib/realloc/

Anyway, I'd write a deep copy function and use it when resizing.

Thanks vernon, I think the purpose of this project is to learn dynamic allocation and the nastiness of linked lists.

So first object read up and play around with some examples of dynamic allocation with a plain array and trying making use of some sort copying of the array, delete array, make new array, place copy and delete copy hmm, this will be fun

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.