I am trying to create a a dynamic list with structs. Now I have the first struct where the information is stored for a person. The other struct holds the first struct like this:
Tried my best to visualize it
employeeRegister
---------------- |-------|------|
registerE ------------> |city---|city--|
---------------- |London |Japan-|
numberOfemployee |pin----|pin---|
---------------- |0101010|101010|
------|2|------- |phone--|phone-|
|SAb123-|APPI20|
Now the problem for me is that I have coded it, and it works but not in the right way as in the image provided.
code :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct personalInfo {
char city[20];
int pin;
char phone[14];
};
struct employeeRegister {
struct personalInfo registerE;
int numberOfemployee;
};
struct employeeRegister *allocateMemory(struct employeeRegister *oldMemory, int nrOfadd);
void addding(struct employeeRegister *addressToAdd, int *nrOfadd);
void printList(struct employeeRegister *allemp, int nrOfadd);
int main(void)
{
struct employeeRegister *employee = NULL;
int choice;
int nrOfaddress = 0;
do
{
printf("
1 - add employee");
printf("
2 - print employee list");
printf("
3 - exit");
printf("
What do you want to do? ");
scanf("%d", &choice);
while (getchar() != '
');
switch (choice)
{
case 1:
employee = allocateMemory(employee, nrOfaddress);
if (employee == NULL)
break;
addding(&employee[nrOfaddress], &nrOfaddress);
break;
case 2:
printList(employee, nrOfaddress);
break;
case 3:
printf("Ending!
");
free(employee);
employee = NULL;
break;
default:
printf("Invalid input
");
break;
}
} while (choice != 3);
return 0;
}
struct employeeRegister *allocateMemory(struct employeeRegister *oldMemory, int nrOfadd)
{
struct employeeRegister *tempurary;
if (nrOfadd == 0)
tempurary = (struct employeeRegister *)calloc(1, sizeof(*tempurary));
else
tempurary = (struct employeeRegister *)realloc(oldMemory, sizeof(*tempurary)*(nrOfadd + 1));
return tempurary;
}
void addding(struct employeeRegister *addressToAdd, int *nrOfadd)
{
printf("City: ");
fgets(addressToAdd->registerE.city, 20, stdin);
addressToAdd->registerE.city[strlen(addressToAdd->registerE.city) - 1] = '';
do
{
printf("Pin: ");
fflush(stdin);
}
while ((scanf("%d", &addressToAdd->registerE.pin) != 1));
while (getchar() != '
');
printf("Phone type: ");
fgets((addressToAdd->registerE.phone), 14, stdin);
addressToAdd->registerE.phone[strlen(addressToAdd->registerE.phone) - 1] = '';
(*nrOfadd)++;
addressToAdd->numberOfemployee = *nrOfadd;
}
void printList(struct employeeRegister *allemp, int nrOfadd)
{
for(int i = 0; i < nrOfadd; i++)
{
printf("%d. %-15s%-5.1d%s
", i + 1, allemp[i].registerE.city,
allemp[i].registerE.pin, allemp[i].registerE.phone);
}
}
When I debug the code I get this:
employeeRegister employeeRegister
---------------- |-------| ---------------- |-------|
registerE ------------> |city---| registerE ------------> |city---|
---------------- |London | ---------------- |London |
numberOfemployee |pin----| numberOfemployee |pin----|
---------------- |0101010| ---------------- |0101010|
------|1|------- |phone--| ------|2|------- |phone--|
|SAb123-| |SAb123-|
When I run the code in codeblocks, everything looks fine in the command prompt, but as you can see, it is not. How would I edit my code to achieve the result of the first image?
It is my first time posting and if there is anything wrong, let me know.