hi friend,
when I input the employee post memory error occurred.
Please help me, thanks.
Code is as follows:
#include
#include
#define EMP_NUM_MAX 100
struct EmployeeInfo // employee information structure
{
char name[15];
int empID;
float salary;
char post[15];
}emp[EMP_NUM_MAX];
// function prototype declaration
void MainMenu(),void WriteData();
// main function
void main(void)
{
int choice;
MainMenu();
printf("\t\tPlease enter your choice:");
scanf("%d",&choice);
switch (choice)
{
case 1:
WriteData();
break;
}
}
void MainMenu() // operator menu
{
system("cls"); // clear the screen
printf("\n\t\t **********************************************\n");
printf("\t\t * Employee Salary Management System For V1.0 *\n");
printf("\t\t **********************************************\n\n");
printf("\t\t 1.Add Employee Information\n");
printf("\t\t 2.Display All Employee Information\n");
printf("\t\t 3.Delete Employee Information\n");
printf("\t\t 4.Find Employee Records\n\n");
}
void WriteData()
{
FILE *fp;
int emp_num;
printf("\t\t You want to input the number of employees?:");
scanf("%d",&emp_num);
for(int i=0;i
{
printf("\t\tPlease input employee name:");
scanf("%s",emp[i].name);
printf("\t\tPlease input employee ID:");
scanf("%d",&emp[i].empID);
printf("\t\tPlease input employee salary:");
scanf("%f",&emp[i].salary);
printf("\t\tPlease input employee post:"); // here occurred error
scanf("%s",emp[i].post);
}
if((fp=fopen("EmployeeInfo.dat","ab"))==NULL) // open binary file
{
printf("Can't open file\n");
exit(1);
}
for(int j=0;j
{
if(fwrite(&emp[j],sizeof(struct EmployeeInfo),1,fp)!=1) //writing data to binary file
printf("Error writing file.\n");
}
fclose(fp); // close file point
}
void DeleteEmployeeInfo()
{
int empID;
printf("Please enter the employee's ID to be deleted:");
scanf("%d",&empID);
/*
here i want to delete the employee ID
*/
}
7 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

VulpesPosted Jul 9, 2013, 9:18 AM
void MainMenu(), void WriteData();
to this:
void MainMenu(), WriteData();
As regards the run-time error, the one I'm getting just before the line you've marked is:
This can be fixed by initializing the float field to (say) zero before calling scanf which forces floating point support to be loaded.
I've also added one or two other lines to improve the format of the display:
Ken HPosted Jul 10, 2013, 11:43 PM
my problem have been solved.thank for Vulpes. :)
Regards.
Ken.
VulpesPosted Jul 10, 2013, 12:20 PM
Ken HPosted Jul 10, 2013, 5:54 AM
void DeleteEmpInfo()
{
FILE *fp;
int empID;
printf("\n\t\tPlease input you want delete employee id:");
scanf("%d",&empID);
}
thanks.
Ken HPosted Jul 10, 2013, 5:10 AM
thank for your answer.:)
Regards.
ken
VulpesPosted Jul 10, 2013, 4:56 AM
I'd replace:
scanf("%s",emp[i].post);
with these two lines:
The first of these lines consumes the new line character left in the buffer by the previous scanf call.
On the second point, you need to check the return value of fread to check that it's actually reading some bytes:
Ken HPosted Jul 9, 2013, 10:14 PM
thank for your answer, and now I have encountered two problems:
1.when i input the "post", if the input is two words, it will eventually save the first word will be truncated after the second, but I have "post" array is set to 20 and it should not happen of error.
2.suppose i just input the two records, and when I choose to display all the employee information,but it outputs the three records.
code is as follows: