Programming with functions and structures using C, getting strange output, no errors

I am a little versed in C and I am doing an exercise when I use structures and functions to collect information about employees and just print them out. I declare the functions and structure in the header, since I need them in both other files.

//employee.h int addEmployee(void); int printEmployee(int i); struct employeelist { char last [20]; char first[20]; int pnumber; int salary; }; 


The "main" file performs both functions. The first function queries the number of employees and returns the value for the second function, in which information is collected and stored. There are no errors, and I checked the code several times.

 //employee.c #include "employee.h" #include <stdio.h> #include <ctype.h> #include <string.h> int numE; int i; int addEmployee(void) { struct employeelist employee[5]; int numE; printf("How many employees do you want to add to the list?: "); scanf("%d", &numE); printf("Please type in the last name, the first name,\nthe personal number and the salary of your employees.\n"); for (i=0; i < numE; i++) { printf("Last name: "); scanf("%s", employee[i].last); printf("First name: "); scanf("%s", employee[i].first); printf("Personal number: "); scanf("%d", &employee[i].pnumber); printf("Salary: "); scanf("%d", &employee[i].salary); } return numE; } int printEmployee(int emp) { struct employeelist employee[5]; for (i=0; i < emp; i++) { printf("Last name: {%s}\nFirst name: {%s}\nPersonal number: {%d}\nSalary: {%d}\n",employee[i].last,employee[i].first, employee[i].pnumber, employee[i].salary); } getchar(); getchar(); return emp; } 


The last file contains the main () function to perform the above functions.

 #include "employee.h" #include <stdio.h> #include <ctype.h> #include <string.h> int emp; int main () { struct employeelist employee[5]; int emp = addEmployee(); printEmployee(emp); return 0; } 


Now my problem is that everything works only on the output incorrectly. I can’t even say what it is. Some random combination of signs, letters and numbers. Since I have no idea where my mistake is, I would welcome any advice to solve this problem. Thanks. alt text


I added a screenshot of my output. Perhaps this helps.

+4
source share
6 answers

You are trying to use a local variable that ceases to exist when the function returns.

 int addEmployee(void) { struct employeelist employee[5]; /* ... */ } 

The variable employee exists only inside the addEmployee () function; and every time a function is called, it is a different object.

 int printEmployee(int emp) { struct employeelist employee[5]; /* ... */ } 

This employee has nothing to do with addEmployee.


But now do not do the easy thing (*); do the right thing : declare an array in the main () function and pass it.

 //employee.h struct employeelist { char last [20]; char first[20]; int pnumber; int salary; }; /* add up to `maxemp` employees to the array and ** return number of employees added */ int addEmployee(struct employeelist *, int maxemp); /* print all employees from index 0 to (nemp - 1) */ int printEmployee(struct employeelist *, int nemp); 

Declare an array in main () and pass it

 #include "employee.h" #include <stdio.h> #include <ctype.h> #include <string.h> int main () { int emp; struct employeelist employee[5]; int emp = addEmployee(employee, 5); printEmployee(employee, emp); return 0; } 

(*) Do not declare a global variable for employees


Edited employee.h and added main () function

+3
source

You have a new variable in your print function that is not even initialized. Therefore, it contains only garbage and is printed

+2
source

The heart of the problem is that you are working with three different arrays of employees, not one array that you think you have. Everything that is declared in a pair of curly braces in C exists only if the program executes the code inside these curly braces. You declared three different arrays of employee structures in three different blocks of code.

To point you in the right direction (whether it’s homework or self-study, this is a training exercise, so just fixing the code is probably not what you want), you should think about how you can do the same the most array of employees to each of your functions in turn. (EDIT: pmg beat me before that.)

Declare an array once in your main function (), as you already did, and change the two functions printEmployee() and addEmployee() to take this array as a parameter instead of declaring your own local arrays.

Here is a very simple example of using ints showing how parameters and return values ​​work. You will need to read a little about pointers to extend this to your case.

 int main() { int number = 2; int newNumber; newNumber = timesTwo(number); printf("number: %d newNumber: %d", number, newNumber); } int timesTwo(int param) { return param * 2; } 

Before you begin to understand separate files and work with structures and pointers, it is useful to practice the concepts of functions, parameters and variables by creating simple programs in one file.

+2
source

You declare an employee array locally for both functions, so they cannot access other data.

+1
source

The main problem, as others have already said, is that you declare a struct employeelist employee[5] in each of your functions. This makes these variables local to their functions and therefore inaccessible from one function to another. Thus, when printEmployee is sent to print, the data it prints is gibberish.

There are several things that need to be fixed:

  • Make one announcement of your list of employees in your main (which you already have), and then pass it to functions. You will need to perform the transfer using the name of the array so that the functions correctly handle the array.
  • As a result, you must move the structure declaration above the function prototypes and then change the prototypes in employee.h to accommodate the passage of the employee-employer.
  • You will need to verify that the user is not trying to enter more entries than your array. Alternatively, you can simply declare the pointer in main and malloc () the required memory in the addEmployee function.

A few other suggestions to help you simplify things a bit:

  • Call your employee structure (or something like that) and your employeelist array (or something similar).
  • Use typedef in your structure to shorten and simplify your ads.
  • Change the operation of printEmployee to the void function (i.e. void return type).
+1
source

Take a look. Well distributed and easy to read, it just needs a little improvement to get bigger and do the work you want.

 typedef struct { char last [20]; char first[20]; long int pnumber; int salary; }employee; /* the two functions clearly declared */ int addEmployee(employee const wEmployeeList[]); void printEmployee( employee const wEmployeeList[], int ); int main() { int numberE; employee employeeList[2]; numberE = addEmployee(employeeList); printEmployee(employeeList, numberE); return 0; } int addEmployee(employee const wEmployeeList[2]){ int i; for (i=0; i < 2; i++) { printf("Last name: "); scanf("%s", wEmployeeList[i].last); printf("First name: "); scanf("%s", wEmployeeList[i].first); printf("Personal number: "); scanf("%ld", &wEmployeeList[i].pnumber); printf("Salary: "); scanf("%d", &wEmployeeList[i].salary); } return 2; } void printEmployee(employee const wEmployeeList[2], int numberEmployee){ int i; for (i=0; i < numberEmployee; i++) { printf("\n\nLast name: %s\nFirst name: %s\nPersonal number: {%ld}\nSalary: {%d}\n \n",wEmployeeList[i].last,wEmployeeList[i].first, wEmployeeList[i].pnumber, wEmployeeList[i].salary); } } 
0
source

All Articles