I'm having some trouble figuring out how to declare structs.
In "struct dep toys", rather then actually initializing the value to "Roger", i am trying to point the value to the "struct emp manager.empname" value. If I leave the value in "dep toys" to "Roger", it works fine, but it's not what the question in the book is asking me to do. I need to reference it to "emp manager.empname".
Should I be using a strcpy somewhere along the line?
secondly, I need to be able to have a function recieve a struct and print out the values. The functions "print_emp" has to take in a type "struct emp" value and us it's contents as values to be displayed.
Thanks.
======================================
#include <stdio.h>
#include <string.h>
struct date
{
int yy,
mm,
dd;
};
struct emp
{
char empname[10];
float salary;
struct date hired;
};
struct dep
{
struct emp manager;
struct emp worker[15];
float profits;
};
void print_emp(struct emp toys);
void main(void)
{
struct date hired;
struct emp manager ={"Roger", 30000,
{hired.yy =1998,
hired.mm = 02 ,
hired.dd =28}
};
struct emp worker[15] = { {"Mojax", 10000,
{hired.yy =1987,
hired.mm = 04 ,
hired.dd =12}},
{"Kojax",8000,
{hired.yy =1980,
hired.mm = 04 ,
hired.dd =02}}
};
struct date date1 = {1934, 02, 23};
struct emp person1 = {"Roger", 30000,
{hired.yy =1998,
hired.mm = 02 ,
hired.dd =28}
};
struct dep toys[2]= {
//******------>problem here {{manager.empname,30000,
{hired.yy =1998,
hired.mm = 02 ,
hired.dd =28}
},
{"Mojax", 10000,
{hired.yy =1987,
hired.mm = 04 ,
hired.dd =12}},
80000},
{{"Roger",30000,
{hired.yy =1998,
hired.mm = 02 ,
hired.dd =28}
},
{"Kojax",8000,
{hired.yy =1980,
hired.mm = 04 ,
hired.dd =02}},
80000}
};
printf("\ndate1: %d/%d/%d\n", date1.mm, date1.dd, date1.yy);
printf("\nPerson1\n---------\nName: %s\nSalary: %.2f\nHired: %d/%d/%d\n",
person1.empname,person1.salary,person1.hired.mm, person1.hired.dd, person1.hired.yy);
printf("\nmanager******%s %.2f %d/%d/%d\n", manager.empname, manager.salary, manager.hired.mm, manager.hired.dd, manager.hired.yy);
printf("\nworker[0]******%s %.2f %d/%d/%d", worker[0].empname, worker[0].salary, worker[0].hired.mm, worker[0].hired.dd, worker[0].hired.yy);
printf("\nworker[1]******%s %.2f %d/%d/%d\n", worker[1].empname, worker[1].salary, worker[1].hired.mm, worker[1].hired.dd, worker[1].hired.yy);
printf("\ntoy[0]******%s %.2f %d/%d/%d", toys[0].manager.empname, toys[0].manager.salary, toys[0].manager.hired.mm, toys[0].manager.hired.dd, toys[0].manager.hired.yy);
printf("\ntoy[0]******%s %.2f %d/%d/%d\n", toys[0].worker[0].empname, worker[0].salary, worker[0].hired.mm, worker[0].hired.dd, worker[0].hired.yy);
printf("\ntoy[1]******%s %.2f %d/%d/%d", toys[1].manager.empname, toys[1].manager.salary, toys[1].manager.hired.mm, toys[1].manager.hired.dd, toys[1].manager.hired.yy);
printf("\ntoy[1]******%s %.2f %d/%d/%d\n", toys[1].worker[1].empname, worker[1].salary, worker[1].hired.mm, worker[1].hired.dd, worker[1].hired.yy);
// print_emp(toys[0]);
// print_emp(toys[1]);
return 0;
}
void print_emp(struct emp toys)
{
printf("\nPersonX\n---------\nName: %s\nSalary: %.2f\nHired: \n",
toys.empname,toys.salary);
}
In "struct dep toys", rather then actually initializing the value to "Roger", i am trying to point the value to the "struct emp manager.empname" value. If I leave the value in "dep toys" to "Roger", it works fine, but it's not what the question in the book is asking me to do. I need to reference it to "emp manager.empname".
Should I be using a strcpy somewhere along the line?
secondly, I need to be able to have a function recieve a struct and print out the values. The functions "print_emp" has to take in a type "struct emp" value and us it's contents as values to be displayed.
Thanks.
======================================
#include <stdio.h>
#include <string.h>
struct date
{
int yy,
mm,
dd;
};
struct emp
{
char empname[10];
float salary;
struct date hired;
};
struct dep
{
struct emp manager;
struct emp worker[15];
float profits;
};
void print_emp(struct emp toys);
void main(void)
{
struct date hired;
struct emp manager ={"Roger", 30000,
{hired.yy =1998,
hired.mm = 02 ,
hired.dd =28}
};
struct emp worker[15] = { {"Mojax", 10000,
{hired.yy =1987,
hired.mm = 04 ,
hired.dd =12}},
{"Kojax",8000,
{hired.yy =1980,
hired.mm = 04 ,
hired.dd =02}}
};
struct date date1 = {1934, 02, 23};
struct emp person1 = {"Roger", 30000,
{hired.yy =1998,
hired.mm = 02 ,
hired.dd =28}
};
struct dep toys[2]= {
//******------>problem here {{manager.empname,30000,
{hired.yy =1998,
hired.mm = 02 ,
hired.dd =28}
},
{"Mojax", 10000,
{hired.yy =1987,
hired.mm = 04 ,
hired.dd =12}},
80000},
{{"Roger",30000,
{hired.yy =1998,
hired.mm = 02 ,
hired.dd =28}
},
{"Kojax",8000,
{hired.yy =1980,
hired.mm = 04 ,
hired.dd =02}},
80000}
};
printf("\ndate1: %d/%d/%d\n", date1.mm, date1.dd, date1.yy);
printf("\nPerson1\n---------\nName: %s\nSalary: %.2f\nHired: %d/%d/%d\n",
person1.empname,person1.salary,person1.hired.mm, person1.hired.dd, person1.hired.yy);
printf("\nmanager******%s %.2f %d/%d/%d\n", manager.empname, manager.salary, manager.hired.mm, manager.hired.dd, manager.hired.yy);
printf("\nworker[0]******%s %.2f %d/%d/%d", worker[0].empname, worker[0].salary, worker[0].hired.mm, worker[0].hired.dd, worker[0].hired.yy);
printf("\nworker[1]******%s %.2f %d/%d/%d\n", worker[1].empname, worker[1].salary, worker[1].hired.mm, worker[1].hired.dd, worker[1].hired.yy);
printf("\ntoy[0]******%s %.2f %d/%d/%d", toys[0].manager.empname, toys[0].manager.salary, toys[0].manager.hired.mm, toys[0].manager.hired.dd, toys[0].manager.hired.yy);
printf("\ntoy[0]******%s %.2f %d/%d/%d\n", toys[0].worker[0].empname, worker[0].salary, worker[0].hired.mm, worker[0].hired.dd, worker[0].hired.yy);
printf("\ntoy[1]******%s %.2f %d/%d/%d", toys[1].manager.empname, toys[1].manager.salary, toys[1].manager.hired.mm, toys[1].manager.hired.dd, toys[1].manager.hired.yy);
printf("\ntoy[1]******%s %.2f %d/%d/%d\n", toys[1].worker[1].empname, worker[1].salary, worker[1].hired.mm, worker[1].hired.dd, worker[1].hired.yy);
// print_emp(toys[0]);
// print_emp(toys[1]);
return 0;
}
void print_emp(struct emp toys)
{
printf("\nPersonX\n---------\nName: %s\nSalary: %.2f\nHired: \n",
toys.empname,toys.salary);
}