MY code is only displaying the input which i entered at the very end. I have attached my full code. PLEASE help me find where i am doing it wrong

288 views Asked by At

QUESTION: WAP to enter id, name, age and basic salary of n number of employees. Calculate the gross salary of all the employees and display it along with all other details in a tabular form, using pointer to structure.

    --->

#include <stdio.h>
#include <conio.h>
 
struct employee {
char name[100];
int id, age;
int salary;
};
 
int main(){
   struct employee emp, *ptr;
    
int i,n;
    printf("Enter the no of employees\n");
    scanf("%d",&n);
    printf("****************INPUT EMPLOYEE DETAILS******************\n");
    for(i=0;i<n;i++)
    {
    printf("\nEnter employee id of employee %d : ",i+1);
    scanf("%d", &emp.id);

    printf("Enter name of employee %d : ", i+1);
    scanf("%s", &emp.name);

    printf("Enter age of employee %d : ", i+1);
    scanf("%d", &emp.age);

    printf("Enter salary of employee %d : ", i+1);
    scanf("%d", &emp.salary);
        
    }
printf("\n");
    printf("                   DISPLAYING EMPLOYEE DETAILS                       \n");
    printf("*********************************************************************\n");

   ptr = &emp;

  printf("\nEMP_ID\t\tEMP_NAME\tEMP_AGE\t\tGROSS_SAL\n");

  for(i=0;i<n;i++){
   printf("%d\t\t%s\t\t%d\t\t%d\n",ptr->id, ptr->name, ptr->age, ptr->salary);
  }
   return 0;
}
1

There are 1 answers

4
javad On

First of all you need to save each of your employees in variable so you can refer them later. Second because your variable is structure and you want loop through it your best choice is to use array. I changed your code and highlighted them it worked for me i hope it will help you.

#include <stdio.h>
 #include <conio.h>
 
  struct employee {
  char name[100];
  int id, age;
  int salary;
 };
 
int main(){
   struct employee *ptr; //One change is here
    
int i,n;
    printf("Enter the no of employees\n");
    scanf("%d",&n);
    struct employee emp[n];
    printf("****************INPUT EMPLOYEE DETAILS******************\n");
    for(i=0;i<n;i++)
    {
    printf("\nEnter employee id of employee %d : ",i+1);
    scanf("%d", &emp[i].id);

    printf("Enter name of employee %d : ", i+1);
    scanf("%s", emp[i].name); // Another change is here "%s" doesn't need '&'

    printf("Enter age of employee %d : ", i+1);
    scanf("%d", &emp[i].age);

    printf("Enter salary of employee %d : ", i+1);
    scanf("%d", &emp[i].salary); 
        
    }
printf("\n");
    printf("                   DISPLAYING EMPLOYEE DETAILS                       \n");
    printf("*********************************************************************\n");

  printf("\nEMP_ID\t\tEMP_NAME\tEMP_AGE\t\tGROSS_SAL\n");

  for(i=0;i<n;i++){
    ptr = &emp[i];   // This yet another change i grab "ptr" down here and assign it to "i" in each loop
   printf("%d\t\t%s\t\t%d\t\t%d\n",ptr->id, ptr->name, ptr->age, ptr->salary);
  }
   return 0;
}