-1

I'm working on a program in C language which collects data for the employees such as names, rates and hours. The thing is I need to use struct.

I am trying to pass struct array into the load() function where user will put names, hours and rates of the employee but it seems I'm doing it wrong. Can you tell me what is wrong?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size 3
int i = 0;
struct Employee
{
    char name[20];
    float rate;
    float hours;

}employee;


void load(struct Employee *guys);
void printout();
void edit();
void realedit();
void print();

int main(void)
{   
    int x=0;
    struct Employee guys[size];


    while (x != 5)
    {
        printf("Main Menu\n");
        printf("1. Add Employee\n");
        printf("2. Edit Employee\n");
        printf("3. Print Employee\n");
        Printf("4. Print All Employees\n");
        printf("5. Quit");
        scanf_s("%d", &x);

        switch (x) 
        {
        case 1: load(guys[i]);
            break;


        default: printf("Please enter valid option.\n\n");
            break;
        }
    }


    system("pause");
    return 0;

}

void load(struct Employee *guys)
{
    puts("\ntype name:\n");
    scanf_s("%s", &guys[i]->name, 20);

    puts("\ntype hourly rate:\n");
    scanf_s("%f", &guys[i]->rate);

    puts("\ntype hours worked:\n");
    scanf_s("%f", &guys[i]->hours);


    i++;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Young KIm
  • 15
  • 3
  • When you are passing guys[i] to load, have load retreive Employee &emp. Then simply store the data using emp.name. Note that it is not good practice to have ' i ' as a global variable. – alhinai-hamed Jul 29 '18 at 04:17
  • 1
    Here `load(guys[i]);` passing structure by value but `load()` parameter expect a pointer to the `struct Employee`. – H.S. Jul 29 '18 at 04:17
  • 1
    Don't forget to check the the `scanf_s()` calls succeed; the one in `main()` should be checked to ensure it returns 1, otherwise there is a problem. – Jonathan Leffler Jul 29 '18 at 04:55
  • A *global variable* of `int i = 0;` is just asking for trouble. – David C. Rankin Jul 29 '18 at 06:33

1 Answers1

4

With the

load(guys[i]);

in your program, you are passing the ith element of the guys array to load() by value. So what's being used in load() is actually a copy changes to which will not affect the original value back in main().

Instead you could pass a pointer pointing to the ith element to load() like

load(&guys[i]);

And use the -> operator to access the variables inside the struct.

void load(struct Employee *guys)
{
    puts("\ntype name:\n");
    scanf_s("%s", guys->name, 20);

    puts("\ntype hourly rate:\n");
    scanf_s("%f", guys->rate);

    puts("\ntype hours worked:\n");
    scanf_s("%f", guys->hours);
}

And no need to use [i] for indexing as we take care of elements one by one.


In

scanf_s("%s", &guys[i]->name, 20);

the function expects the base address of the array. So use

scanf_s("%s", guys[i]->name, 20);

as the name of the array decays into a pointer to the first element of the array.

As for your use of system() in

system("pause");

consider reading Why should the system() function be avoided in C and C++?.

Also, note that puts() will automatically print a \n at the end. So, if you see fit you can do away with a \n.

And you have a small typo. The

Printf("4. Print All Employees\n");

should've been

printf("4. Print All Employees\n");
J...S
  • 5,079
  • 1
  • 20
  • 35