I'm studying coding for some credits in the university, and I was studying and decided to try to make something with structs in C++ to try to understand them better.
I'm trying to make some functions to work with the struct, but they only work if I write & before declaring the arguments, but in the classroom we were told it should not be necessary. My code is the following.
struct employee{
int ID;
char name;
double salary;
};
struct empresa{
char name;
struct employee employee[];
};
void iterate_trough_employees(struct empresa &empresa, int number_employees){
for ( int i = 0; i < number_employees; i++){
empresa.employee[i].ID = i+1;
printf("\n\n EL ID del empleado es:\t %d \n",empresa.employee[i].ID);
empresa.employee[i].salary = rand() % 50;
printf("El salario del empleado es: %.1lf \n", empresa.employee[i].salary);
}
}
void sort_this(struct empresa &empresa, int number_employees){
int list_of_ID[number_employees];
list_of_ID[0]= empresa.employee[4].ID;
printf("\n%d", empresa.employee[4].ID);
printf("\n%d", list_of_ID[0]);
printf("\n%.0lf\n", empresa.employee[6].salary);
}
int main(){
struct empresa empresa;
int empleados = 10;
int orden_salarios[10];
iterate_trough_employees(empresa, empleados);
printf("%d", empresa.employee[6].ID);
sort_this(empresa, empleados);
return 0;
}
I'm mostly interested in the sort_this
function, which only shows the correct values on screen when I write the K (the code in the function is not related to the title, because I have been trying to figure out the mistake making an easier function).
For those asking, we've been taught to code in C using some C++ elements (such as the pass by reference from C++ or cin/cout) to make it easier, and therefore it may seem to you that the code is not C nor C++. I'm studying coding for some credits in the university, and I was studying and decided to try to make something with structs in C++ to try to understand them better.
I'm trying to make some functions to work with the struct, but they only work if I write & before declaring the arguments, but in the classroom we were told it should not be necessary. My code is the following.
struct employee{
int ID;
char name;
double salary;
};
struct empresa{
char name;
struct employee employee[];
};
void iterate_trough_employees(struct empresa &empresa, int number_employees){
for ( int i = 0; i < number_employees; i++){
empresa.employee[i].ID = i+1;
printf("\n\n EL ID del empleado es:\t %d \n",empresa.employee[i].ID);
empresa.employee[i].salary = rand() % 50;
printf("El salario del empleado es: %.1lf \n", empresa.employee[i].salary);
}
}
void sort_this(struct empresa &empresa, int number_employees){
int list_of_ID[number_employees];
list_of_ID[0]= empresa.employee[4].ID;
printf("\n%d", empresa.employee[4].ID);
printf("\n%d", list_of_ID[0]);
printf("\n%.0lf\n", empresa.employee[6].salary);
}
int main(){
struct empresa empresa;
int empleados = 10;
int orden_salarios[10];
iterate_trough_employees(empresa, empleados);
printf("%d", empresa.employee[6].ID);
sort_this(empresa, empleados);
return 0;
}
I'm mostly interested in the sort_this
function, which only shows the correct values on screen when I write the K (the code in the function is not related to the title, because I have been trying to figure out the mistake making an easier function).
For those asking, we've been taught to code in C using some C++ elements (such as the pass by reference from C++ or cin/cout) to make it easier, and therefore it may seem to you that the code is not C nor C++.