Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3426456
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:43:05+00:00 2026-05-18T06:43:05+00:00

I’m learning a bit C and i’m doing an exercise where i use structures

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:43:05+00:00Added an answer on May 18, 2026 at 6:43 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I have text I am displaying in SIlverlight that is coming from a CMS
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.