Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

I'm learning a bit C and i'm doing an exercise where i use structures and functions to collect employee infos and just print them out. I'm declaring the functions and the struct in the header since i need them in both of the other files.

//employee.h

int addEmployee(void);
int printEmployee(int i);

struct employeelist 
{
    char last [20];
    char first[20];
    int pnumber;
    int salary;
};


The "core" file is executing the both functions. The first function asks for the number of employees and gives a value back for the second function where the information is collected and stored. There are no errors and i've 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 execute the functions above.

#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 the output is incorrect. I can't even say what it is. Some kind of random mix of signs, letters and numbers. Since i have no idea where my mistake is, i would be glad about any advise to solve this problem. Thank you.alt text


I have added a screenshot of my output. Maybe it helps.

like image 854
Ordo Avatar asked Jan 22 '23 00:01

Ordo


2 Answers

You're trying to use a local variable, which ceases to exist when the function returns.

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

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

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

This employee has no relation to the one in addEmployee.


But don't do the easy thing now (*); do the right thing: declare the array in the main() function and pass it around.

//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 around

#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;
}

(*) Don't declare a global variable for employees


Edited employee.h and added a main() function

like image 166
pmg Avatar answered Jan 23 '23 14:01

pmg


You have a brand new variable in your print function that isn't even initialized. So it contains only garbage and that is printed

like image 35
Jens Gustedt Avatar answered Jan 23 '23 14:01

Jens Gustedt