Originally Posted by
whiteflags
> Hi ..
Hello.
> Not to sure about Structures but would this be how its done
novacain's answer is the most complete, whereas your attempt has all of the problems already discussed.
> I don't think you can actually put a calculation in a structure as such.
It is important to try to interpret questions from people in such a way that an answer is possible. Many times, just explaining what you can do (as novacain did) or asking for clarification (as Nominal Animal did) moves the thread in a productive direction.
I'm sorry my bad, I'm in a hurry when I post...
What I want really to do is to create a database, an employee database specifically, containing:
1. ID Number
2. Name
3. How many hour the employee works overnight
4. How many hour the employee has been absent
5. Bonus -> where Bonus = overnight-((2/3)*absent)
And I should be able to:
1. Insert a record
2. Display the records
3. Modify a record
4. Delete a record
The problem is I want the program to automatically calculate the "Bonus" when the records are displayed. Here is the code so far
Code:
#include<stdio.h>
#include<stdlib.h>
typedef struct employee
{
char name[30];
int empid,overnight,absent;
}emp;
void insert(emp e[50],int n)
{
printf("\nEnter the name of the employee: ");
scanf("%s",&e[n].name);
printf("Enter the Employee ID: ");
scanf("%d",&e[n].empid);
printf("Enter how many hour(s) the employee works overnight: ");
scanf("%d",&e[n].overnight);
printf("Enter how many hour(s) the employee has been absent: ");
scanf("%d",&e[n].absent);
}
void display(emp e[50],int n)
{
int i;
printf("\nEMP ID\tNAME\tOVERNIGHT\tABSENT\tBONUS");
printf("\n**************************************\n");
for(i=0;i<n;i++)
{
printf("\n%d\t%s\t%d hour\t%d hour\t???\n",e[i].empid,e[i].name,e[i].overnight,e[i].absent);
printf("\n**************************************\n");
}
}
void search(emp e[50],int n)
{
int i,id;
printf("\nEnter the Employee ID to be searched: ");
scanf("%d",&id);
for(i=0;i<n;i++)
{
if(e[i].empid==id)
{
printf("\nRecord present.");
printf("\n>> %d\t%s\t%d\t%d",e[i].empid,e[i].name,e[i].overnight,e[i].absent);
return;
}
}
printf("\nRecord not present.\n");
}
void modify(emp e[50],int n)
{
int i,id;
printf("\nEnter the Employee ID to be modified: ");
scanf("%d",&id);
for(i=0;i<n;i++)
{
if(e[i].empid==id)
{
printf("\nRecord present.\n");
printf("\nEnter the new name: ");
scanf("%s",&e[n].name);
printf("Enter the new overnight hour: ");
scanf("%d",&e[i].overnight);
printf("Enter the new absent hour: ");
scanf("%d",&e[i].absent);
printf("\nRecord modified.\n");
return;
}
}
printf("\nRecord not present.\n");
}
int delrec(emp e[50],int n)
{
int i,id;
printf("\nEnter the Employee ID to be deleted: ");
scanf("%d",&id);
for(i=0;i<n;i++)
{
if(e[i].empid==id)
{
break;
}
}
if(i==n)
{
printf("\nRecord not present.\n");
}
else
{
printf("\nRecord present and deleted.\n");
while(i<n)
{
e[i]=e[i+1];
i++;
}
n=n-1;
}
return(n);
}
int main(int argc, char **argv)
{
emp e[50];
int n=0,ch;
printf("=================\n");
printf("EMPLOYEE DATABASE\n");
printf("=================\n");
while(1)
{
printf("\n1] Insert Data\n2] Display Data\n3] Search Data\n4] Modify Data\n5] Delete Data\n6] Exit");
printf("\nPlease enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:insert(e,n);
n=n+1;
break;
case 2:display(e,n);
break;
case 3:search(e,n);
break;
case 4:modify(e,n);
break;
case 5:n=delrec(e,n);
break;
case 6:exit(0);
default:printf("\nPlease enter the correct choice.\n");
}
}
}
/* This is the condition of the bonus
int bonus;
if(e[n].b>40) {
bonus = 5000;
}
else if((e[n].b>30)&&(e[n].b<=40)) {
bonus = 4000;
}
else if((e[n].b>20)&&(e[n].b<=30)) {
bonus = 3000;
}
else if((e[n].b>10)&&(e[n].b<=20)) {
bonus = 2000;
}
else {
bonus = 1000;
}
*/
Thank you all for your answers