# Complex numbers and C structures

This is a discussion on Complex numbers and C structures within the C Programming forums, part of the General Programming Boards category; Originally Posted by overlord21 hello guys i got this exercice in which i need help. This program should fill two ...

1. Originally Posted by overlord21
hello guys i got this exercice in which i need help. This program should fill two arrays made of information about students and employee.

i have to sort alphabetically each of them by name and last name, still i have to check that no name should be repeated. In this program i tried to store the information in a table string"std" and in the same time check for any repeated name but i have problem with the sorting and when i run this it shuts down as the size ins't recognized even if i specified it.
Okay we clearly have a lot to do so lets break down the assignment and what we have so far. That's step one when you start to think about programming, by the way. My professor used to tell me that every hour spent planning saves two hours in coding time. Jumping right in and just attempting to debug the whole solution at once is a mess.

You must:
• collect data about students and employees in an array
• delete repeated names
• sort the data alphabetically by surname

Let's see how you tackled the first bullet:
Code:
```typedef struct {
char *firstname;
char *lastname;
}student;

typedef struct {
char *lastname;
char *firstname;
float salary;
}employee;

void fill_e(employee *e)
{
scanf("&#37;s",&e->firstname);
scanf("%s",&e->lastname);
scanf("%f",&e->salary);

}

void fill_s(student *s)
{
scanf("%s",&s->firstname);
scanf("%s",&s->lastname);

}```
While it is good that strudent and employee have name fields that don't impose a hard limit on the length of the name, you forgot completely to allocate memory. Accessing those fields at all will result in undefined behavior and most likely cause a program crash. To avoid that, perhaps you should rely on arrays for now, during development, and introduce malloc'd strings later, when the program is almost done.

And scanf isn't one of the most trustworthy ways to read strings because of the possibility you'll write past an array boundary, invoking undefined behavior again. I really would choose a different approach for input, like fgets() & sscanf(). We can get the information we need using fgets() to fill name fields. To get the grade and salaries, we could use sscanf(). sscanf() just formats input from a string like scanf would.

Also, there isn't a problem with your search, but arrays cannot be resized should you need to remove a record like in bullet one. So you need to search for a person of the same name during the input stage, before the record is added to the array. Then you can ask the user for a different name.

So maybe you can work with this at first:
Code:
```#define NAMESIZE 512
#define SIZE 10

#include <stdio.h>
#include <string.h>

struct EmployeeTag {
float salary;
char firstname[NAMESIZE];
char lastname[NAMESIZE];
};

struct StudentTag {
char firstname[NAMESIZE];
char lastname[NAMESIZE];
};

typedef struct EmployeeTag Employee;
typedef struct StudentTag Student;

Employee * newEmployee (Employee * e, Employee records[SIZE]);
int search (Employee * worker, Employee records[SIZE]);

Employee * newEmployee (Employee * worker, Employee records[SIZE])
{
char input[NAMESIZE * 2];
printf("Enter employee's last name:\n");
if (fgets(input, NAMESIZE, stdin) != NULL) {
char * end = strchr(input, '\n');
if (nl != NULL)
*nl = '\0';
strcpy(worker->lastname, input);
}
printf("Enter the employee's first name:\n");
if (fgets(input->firstname, NAMESIZE, stdin) != NULL) {
char * end = strchr(input, '\n');
if (nl != NULL)
*nl = '\0';
strcpy(worker->firstname, input);
}
printf("Enter the employee's salary:\n");
if (fgets(input, sizeof input, stdin) != NULL) {
sscanf(input, "%f", &worker->salary);
}
if (search(worker, records) == 1)
return NULL;
else
return worker;
}```
Now you can use newEmployee() to fill in any part of the array, and depending on what it returns if it was successful or not. A similar process should be made for students.

Next comes solving bullet two -- sorting. You should select an algorithm and try to implement it.

Let's see what you come up with now. HTH.

2. Originally Posted by Elysia
And don't use conio.h. It's an evil non-standard header.
Nonstandard does not an evil library make. Most compilers that target Win32 or MS-DOS will include a conio header and it really depends on for whom this program is intended and what parts of the library is being used, like any library.

3. the programs bugs out when removing the "&"

4. Where is the memory for lastname and firstname allocated?

5. hey guys, can anyone tell me just( functions or instruction) on how to make the previous exercice using a sequential text file?