Member Avatar for wiliams.kikert

hi.. im new here, and this will be my very first post ever in this website.. i am using Dev C++.. so can you help me out a little here?? i have this code.. yes, some of these codes are found all over the internet.. but most of the code are written by me.. now after all of that research.. i have to to this code!! need to check for some errors here... this is just something for my project.. ive tried this and it work but i am not still conviced that this is error free.. so here it is

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>

void append();
void list();
void search();
void modify();
void del();

struct employee
{
int no, sal;
char gen, name[20];
};

int main()
{ int a;
char ch;
do{
printf("\nEMPLOYEE DATABASE\n\n");
printf("1.Append Employee Record\n2.List Employee Record\n3.Modify Employee Record\n4.Delete Employee Record\n5.Search Employee Record\n Enter Choice : ");
scanf("%d",&a);
switch(a)
{
case 1:
append();
break;
case 2:
list();
break;

case 3:
modify();
break;
case 4:
del();
break;
case 5:
search();
break;
default :
printf("Invalid Choice!");
}
printf("\n More Actions ? (Y/N) :");
fflush(stdin);
scanf("%c", &ch);
}while(ch=='y'|| ch=='Y');
}
void append()
{ int i,n;
struct employee e;
FILE *fp;
fp=fopen("Employee.txt", "a");

if(fp==NULL)
{
printf("File Creation Failed!");
exit(0);
}
printf("Enter the nos. of employees : ");
scanf("%d", &n);
for(i=0;i<n;i++) {
printf("Enter the Employee Number : ");
scanf("%d", &e.no);
printf("Enter the Employee Salary : ");
scanf("%d", &e.sal);
printf("Enter the Employee gender: ");
fflush(stdin);
scanf("%c", &e.gen);
printf("Enter the Employee Name : ");
fflush(stdin);
gets(e.name);
printf("\n\n");

fwrite((char *)&e, sizeof(e), 1, fp);
}
fclose(fp);
}

void list()
{ int nofrec=0;
struct employee e;
FILE *fp;
fp=fopen("Employee.txt", "rb");
if(fp==NULL)
{
printf("\n\tFile doesn't exist!!!TRY AGAIN");
exit(0);
}

while((fread((char *)&e, sizeof(e), 1, fp))==1)
{ nofrec++;
printf("\nEmployee Number : %d", e.no);
printf("\nEmployee Salary : %d", e.sal);
printf("\nEmployee gender : %c",e.gen);
printf("\nEmployee Name : %s",e.name);
printf("\n\n");
}
printf("Total number of records present are : %d", nofrec);

fclose(fp);

}

void modify()
{ int recno, nofrec=0;
char ch;

struct employee e;
FILE *fp;
fp=fopen("Employee.txt", "rb+");

printf("Enter the Employee Number to modify : ");
scanf("%d", &recno);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{ nofrec++;
if(e.no==recno)
{
printf("\nEmployee Number : %d", e.no);
printf("\nEmployee Salary : %d", e.sal);
printf("\nEmployee gender : %c",e.gen);
printf("\nEmployee Name : %s",e.name);
printf("\n");

printf("Do you want to modify this record : ? (Y/N)");
fflush(stdin);
scanf("%c", &ch);

fseek(fp, ((nofrec-1)*sizeof(e)), 0);
if(ch=='Y'|| ch=='y')
{
printf("Enter the Employee Salary : ");
scanf("%d", &e.sal);
printf("Enter the Employee gender: ");
fflush(stdin);
scanf("%c", &e.gen);
printf("Enter the Employee Name : ");
fflush(stdin);
gets(e.name);
fwrite((char *)&e, sizeof(e), 1, fp);
printf("Record Modified");
}

else
printf("No modifications were made");

fclose(fp);

}
}
}
void del()
{
int recno;
char ch;

struct employee e;
FILE *fp, *ft;
fp=fopen("Employee.txt", "rb");
ft=fopen("Temp.dat", "wb");

printf("Enter the Employee Number to delete : ");
scanf("%d", &recno);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
if(e.no==recno)
{
printf("\nEmployee Number : %d", e.no);
printf("\nEmployee Salary : %d", e.sal);
printf("\nEmployee gender : %c",e.gen);
printf("\nEmployee Name : %s",e.name);
printf("\n");

printf("Do you want to delete this record : ? (Y/N)");
fflush(stdin);
scanf("%c", &ch);

}
}
if(ch=='y'||ch=='Y')
{
rewind(fp);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
if(recno!=e.no)
{
fwrite((char *)&e, sizeof(e), 1, ft);
}
}
}
else
printf("No Record was deleted");

fclose(fp);
fclose(ft);
remove("Employee.txt");
rename("Temp.dat", "Employee.txt");

}
void search()
{ int s,recno;
char sname[20];

struct employee e;
FILE *fp;
fp=fopen("Employee.txt", "rb");
printf("\n1.Search by Name\n2.Search by Employee No.\n Enter choice : ");
scanf("%d", &s);
switch(s)
{
case 1:

printf("Enter the Employee Name to Search : ");
fflush(stdin);
gets(sname);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
if(strcmp(sname,e.name)==0)
{
printf("\nEmployee Number : %d", e.no);
printf("\nEmployee Salary : %d", e.sal);
printf("\nEmployee gender : %c",e.gen);
printf("\nEmployee Name : %s",e.name);
printf("\n");

}
}
break;

case 2:

printf("Enter the Employee Number to Search : ");
scanf("%d", &recno);
while((fread((char *)&e, sizeof(e), 1, fp))==1)
{
if(e.no==recno)
{
printf("\nEmployee Number : %d", e.no);
printf("\nEmployee Salary : %d", e.sal);
printf("\nEmployee gender : %c",e.gen);
printf("\nEmployee Name : %s",e.name);
printf("\n");

}
}
break;
}
}

P.S. i am also trying to convert this to java.. can you help me out to link this to the java section??

Ancient Dragon commented: used code tags correctly in first post :) +14

I can't help you with converting to java, but if you expect anyone to actually read your program you need to format it better. Make liberal use of spaces to make your code easier to read. For example, here's the first few lines of your program

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>

void append();
void list();
void search();
void modify();
void del();

struct employee
{
    int no;
    int sal;
    char gen;
    char name[20];
};

int main()
{ 
    int a;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.