0
I’m learning C. However, I had a problem getting through a structure to a function. I cannot understand which is the error.
I will be grateful if someone helps me. Me sorry for the bad documentation. However, the code is very simple.
#include<stdio.h>
#include<stdlib.h>
#define QT_EMP 2
/*Address*/
struct address{
char country[20];
char city[20];
char st[20];
unsigned long int zip;
};
/*Personal Data*/
struct pd{
char name[20];
int age;
float size;
float heavy;
};
/*Employes*/
struct emp{
unsigned freedom: 1;
unsigned is_emp : 1;
struct pd data;
struct address addr;
};
int menu ();
void del (struct emp *p, int i);
void init (struct emp *p);
int check (struct emp *p);
void show (struct emp *p);
void enter (struct emp *p, int i);
/*---------------------*/
/*--------MAIN---------*/
int main() {
struct emp actors[QT_EMP];
int choice = 0;
init(&actors);
while (choice < 1 || choice > 4) {
choice = menu();
switch (choice) {
/*Insert*/
case 1:
{
int i;
i = check(&actors);
if (i != -1) enter(&actors, i);
else printf("\nBad entry!");
break;
}
/*Delete*/
case 2:
{
del();
break;
}
/*Show*/
case 3:
show(&actors);
break;
/*Exit*/
case 4:
exit (1);
}
}
return 0;
}
/*---------------------*/
/*---------------------*/
/*Delete*/
void del (struct emp *p, int i) {
int i;
printf("Type the indice: ");
scanf("%d", &i);
p[i]->is_emp = 0;
}
/*Initiate*/
void init (struct emp *p) {
register int i;
for (i = 0; i < QT_EMP; ++i)
p[i]->freedom = 1;
}
/*Check for empty indice*/
int check (struct emp *p) {
register int i;
for (i = 0; i < QT_EMP; ++i) {
if (p[i]->freedom) return i;
}
return -1;
}
/*Menu*/
int menu () {
int choice = 0;
while (choice < 1 || choice > 4) {
printf("------MENU------");
printf("Select an option:\n");
printf("1) Insert;\n");
printf("2) Delete;\n");
printf("3) Show;\n");
printf("4) Exit. ");
scanf("%d", &choice);
}
return choice;
}
/*Show*/
void show (struct emp *p) {
register int i;
for (i = 0; i < QT_EMP; ++i)
if (p[i]->freedom) {
if (p[i]->is_emp) printf("\nEmployee");
else printf("\nEx Employee!");
printf("\n----Personal Data----");
printf("\nName: %s", p[i]->data.name);
printf("\nAge: %d", p[i]->data.age);
printf("\nSize: %3f", p[i]->data.size);
printf("\nHeavy: %f", p[i]->data.heavy);
printf("\n----Adress----");
printf("\nCountry: %s", p[i]->addr.country);
printf("\nCity: %s", p[i]->addr.city);
printf("\nStreet: %s", p[i]->addr.st);
printf("\nZIP code: %d", p[i]->addr.zip);
}
}
/*Enter the data of emp*/
void enter (struct emp *p, int i) {
p->freedom = 0;
p->is_emp = 1;
/*Personal data*/
printf("\n----Personal Data----");
printf("\nName:");
scanf("%20s", &p[i]->data.name);
printf("\nAge: ");
scanf("%d", &p[i]->data.age);
printf("\nSize: ");
scanf("%f", &p[i]->data.size);
printf("\nHeavy: ");
scanf("%f", &p[i]->data.heavy);
/*Address*/
printf("\n----Adress----");
printf("\nCountry: ");
scanf("%20s", &p[i]->addr.country);
printf("\nCity: ");
scanf("%20s", &p[i]->addr.city);
printf("\nStreet: ");
scanf("%20s", &p[i]->addr.st);
printf("\nCity: ");
scanf("%d", &p[i]->addr.zip);
}
what is going wrong?
– TotallyUncool
Try to cut the code to have only the part that has the problem, as well as the error that accompanies the problem
– MoshMage