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
*/
}

VulpesRank #55 · 29.2K reputationPosted Jul 9, 2013, 5:18 AM EDT
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 HRank #2,315 · 610 reputationPosted Jul 9, 2013, 6:14 PM EDT
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:
VulpesRank #55 · 29.2K reputationPosted Jul 10, 2013, 12:56 AM EDT
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 HRank #2,315 · 610 reputationPosted Jul 10, 2013, 1:10 AM EDT
thank for your answer.:)
Regards.
ken
Ken HRank #2,315 · 610 reputationPosted Jul 10, 2013, 1:54 AM EDT
void DeleteEmpInfo()
{
FILE *fp;
int empID;
printf("\n\t\tPlease input you want delete employee id:");
scanf("%d",&empID);
}
thanks.
VulpesRank #55 · 29.2K reputationPosted Jul 10, 2013, 8:20 AM EDT
Ken HRank #2,315 · 610 reputationPosted Jul 10, 2013, 7:43 PM EDT
my problem have been solved.thank for Vulpes. :)
Regards.
Ken.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts