Why my input gone after input into a structure through a function?

Yong Yung Fook

So below is my program that contain several function about information of employees that were stored within a structure such as display all of them, search by entering ID or date joined and also edit or add new record. So my problem now is after i use the addRecord function within my program, then it navigated back to the menu, when i choose to displayAll, the new employee's information that i have just entered through the addRecord function is all gone, can someone please help me to spot where my mistakes are? Appreciate it a lot.

#include <stdio.h>
#include <string.h>
#define MAX_SIZE 20

struct Date {
    int day;
    int month;
    int year;
};

struct Employee {
    char employeeId[4];
    char name[20];
    struct Date date;
    char department[15];
};

void displayAll(struct Employee em[MAX_SIZE], int employee);

void search(struct Employee em[MAX_SIZE], int employee);

void viewRecord(struct Employee em[MAX_SIZE], int employee);

void editRecord(struct Employee em[MAX_SIZE], int employee);

addRecord(struct Employee em[MAX_SIZE], int employee);

int main()
{
    int nEmployees = 5;
    int menuChoice;

    struct Employee emp[MAX_SIZE] = { { "E01", "Alice Chan", {5, 12, 2008}, "R&D"},
                                      { "E02", "John", {9, 12, 2011}, "IT"},
                                      { "E03", "Vivian", {3, 3, 2015}, "HR"},
                                      { "E04", "Alice Chin", {4, 4, 2011}, "IT"},
                                      { "E05", "Vivien Tan", {5, 3, 2015}, "HR"}
    };
    
    do {
        printf("\n\n\nHello human, what do you want to do? \n");
        printf("1 for display all employee's information. \n");
        printf("2 for search based on date joined. \n");
        printf("3 for search based on ID. \n");
        printf("4 for editing employees' info. \n");
        printf("5 for adding new employee's info. \n");
        printf("0 for exit. \n");
        printf("> ");
        scanf("%d", &menuChoice);
        
        if (menuChoice == 1) {
            displayAll(emp, nEmployees);
        }
        
        else if (menuChoice == 2) {
            search(emp, nEmployees);
        }
        
        else if (menuChoice == 3) {
            viewRecord(emp, nEmployees);
        }
        
        else if (menuChoice == 4) {
            editRecord(emp, nEmployees);
        }
        
        else if (menuChoice == 5) {
            addRecord(emp, nEmployees);
        }
        
        else {
            printf("The choice is invalid :(. Please try again. ");
        }
        
    } while(menuChoice != 0);
    
}

void displayAll(struct Employee em[MAX_SIZE], int employee) {
    int counter = 0;
    
    printf("Employee Details : \n");
    printf("-------------------\n");
    printf("Employee ID    Name           Date Joined    Department\n");
    do {
        printf("%-15s%-15s%-2d%-3d%-10d%-s\n", em[counter].employeeId, em[counter].name, em[counter].date.day, em[counter].date.month, em[counter].date.year, em[counter].department);
        counter++;
    } while(counter < employee);
}
    
void search(struct Employee em[MAX_SIZE], int employee) {
    int counter = 0;
    int search1, search2;
    printf("Enter the month and year to search (exp : 12 2008): ");
    scanf("%d %d", &search1, &search2);
    printf("Employee Details : \n");
    printf("-------------------\n");
    printf("Employee ID    Name           Date Joined    Department\n");
    do {
    if(search1 == em[counter].date.month && search2 == em[counter].date.year) {
        printf("%-15s%-15s%-2d%-3d%-10d%-s\n", em[counter].employeeId, em[counter].name, em[counter].date.day, em[counter].date.month, em[counter].date.year, em[counter].department);
    } 
    counter++;
    } while(counter < employee);
}

void viewRecord(struct Employee em[MAX_SIZE], int employee) {
    int counter = 0;
    char search3[10];
    
    printf("Enter the ID to search : ");
    scanf(" %s", &search3);
    do {
    if(strcmp(search3, em[counter].employeeId) == 0) {
        printf("Employee ID > %s\n", em[counter].employeeId);
        printf("Name        > %s\n", em[counter].name);
        printf("Date Joined > %d-%d-%d\n", em[counter].date.day, em[counter].date.month, em[counter].date.year);
        printf("Department  > %s\n", em[counter].department);
    }
    counter++;
    } while(counter < employee);
}

void editRecord(struct Employee em[MAX_SIZE], int employee) {
    int counter = 0;
    int editChoice;
    char choice[10];
    
    printf("Enter the ID you want to edit > ");
    scanf("%s", &choice);
    do {
    if (strcmp(choice, em[counter].employeeId) == 0) {
    printf("What do you want to edit ? ( 1 for name, 2 for date joined, 3 for department) > ");
    scanf("%d", &editChoice);
    
    if (editChoice == 1) {
        printf("Enter new name > ");
        scanf(" %[^\n]", &em[counter].name);
        }
    
    else if (editChoice == 2) {
        printf("Enter new date joined > ");
        scanf(" %d %d %d", &em[counter].date.day, &em[counter].date.month, &em[counter].date.year);
    }    
    
    else if (editChoice == 3) {
        printf("Enter the department > ");
        scanf(" %[^\n]", &em[counter].department);
    }
    printf("Employee ID > %s\n", em[counter].employeeId);
    printf("Name        > %s\n", em[counter].name);
    printf("Date Joined > %d-%d-%d\n", em[counter].date.day, em[counter].date.month, em[counter].date.year);
    printf("Department  > %s\n", em[counter].department);
    }
    
    counter++;
    } while(counter < employee);
}

addRecord(struct Employee em[MAX_SIZE], int employee) {
    printf("Enter the ID > ");
    scanf(" %[^\n]", &em[employee].employeeId);
    
    printf("Enter the name > ");
    scanf(" %[^\n]", &em[employee].name);
    
    printf("Enter the date joined (exp: 28 8 2002) > ");
    scanf(" %d %d %d", &em[employee].date.day, &em[employee].date.month, &em[employee].date.year);
    
    printf("Enter the department > ");
    scanf(" %s", &em[employee].department);
    
    employee++;
    
    return employee;
}
Antti Haapala

You're never incrementing the nEmployees variable. Presumably you meant to do something like

nEmployees = addRecord(emp, nEmployees);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does my program not quit after I input "q"?

why is my input not triggering my function

Why is my program not accepting input through the console? (Java)

Why does sqrt function not work for values taken through user input?

why my up function changes input out of its block in Java

Why is my function modifying the input variable?

Why does my function shift my input data?

Length of line in function defaults to 1 after first user input, why?

Why does my input function doesn't get input correctly?

Why is the execution of my binary search script not proceeding after input?

Input function converts the input into string so that is why my list input does not work

Why doesn't my function detect a correct input selection?

Why is SED not accepting my input?

Why input fields get disabled after ajax or javascript function call?

Why is my foreach input not set?

Avoid structure input in function

Why is my screen going black after a minute or less of no input activity?

Why is my function running without asking for the input first?

Why won't my user input field reset after submitting?

Why does my defined function not accept an array as an input?

Why does my code skip input prompts after the second one?

Why does my program exit after taking a input

Why does my loop end after the input is entered?

Why my function breaks when I input a large text value?

Why is my Python script printing "None" after using a function and using input(print())?

Why is my input validation function not working? (C++)

Why my autocomplete input is not working?

Why is my scanner not accepting any input after my user prompt?

Why is my sorting function returning more values than input