I am a newbie with C and I am trying to run a program from my book which shows how we deal with arrays of structures.
#include<stdio.h>
#include<conio.h>
struct employee
{
int empno;
char name[30];
int basic;
int hra;
};
void main()
{
struct employee e[50];
int i, j, n;
int net[50];
float avg;
printf("Enter the number of employees: ");
scanf("%d", &n);
printf("Enter Emp. No. \tName:\tBasic\tHRA of each employee in the order.\n");
for(i=0; i<n; i++)
{
scanf("%d", &e[i].empno);
gets(e[i].name);
scanf("%d", &e[i].basic);
scanf("%d", &e[i].hra);
net[i]=e[i].basic + e[i].hra ;
avg = avg + net[i];
}
avg = avg/n;
printf("Emp. No \t Name-Netpay: ");
for(i=0; i<n; i++)
{
if(net[i]>avg)
{
printf("\t",e[i].empno);
printf("\t", e[i].name);
printf("\t", net[i]);
} } }
I also have further modules which goes on to compute the average and prints those elements whose salary + hr is more than average. However the code pasted above does not work as intended.
Now, if I enter the number of employees - Let's say 1, it only allows me to enter the empno and name and exits the loop. I am expecting it to complete at least one cycle through the loop with the value 1.
Any suggestions on this would be highly appreciated and I apologize if I am messing up anywhere. Thanks.