求助:Unhandled exception in Graph.exe:0xC0000005:Access Violation

lastnemo 2011-04-15 03:56:50
编译和运行都可以,不过在选择是infs或是fs,还有用编号查询的时候,这两种情况的时候出现错误
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>

typedef struct Emp
{
int EmpId; // 员工编号
char EmpName[200]; // 姓名
char Position[200]; // 职务
int Pay; // 工资
int SalesVolume; // 销售量
int GrossSales; // 销售总额
}Emp;

struct
{
Emp employee[100];
int usedFlag[100]; // 这个标志为用于标志相同索引的 employee数组中的变量 是否被使用。 1 --- 使用了, 0 --- 未使用
int sortByWorkYears[100];
}EmpArray;

FILE *fw;


void menu()
{
printf("\n**************************MENU********************\n");
printf("V|v ---- 显示:\n");
printf("F|f ---- 查询:\n");
printf("I|i ---- 录入:\n");
printf("D|d ---- 删除:\n");
printf("C|c ---- 修改:\n");
printf("S|s ---- 保存:\n");
printf("Q|q ---- 退出程序\n");
printf("请选择操作的种类:\n");

}

void menuFind()
{
printf("a. 按姓名查找职工记录\n");
printf("b. 按编号查找职工记录\n");
printf(" 其他键返回上级菜单! \n");
printf("请选择操作的种类:\n");
}

Emp* AllocateEmp()
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (0 == EmpArray.usedFlag[i])
{
EmpArray.usedFlag[i] = 1;
return &EmpArray.employee[i];
}
}
return 0;
}


void show(Emp* pEmp)
{
printf("\n-------------------------员工信息---------------------------\n");
printf("员工号:%d\n",pEmp->EmpId);
printf("姓名:%s\n", pEmp->EmpName);
printf("职务:%s\n",pEmp->Position);
printf("工资:%d\n",pEmp->Pay);
printf("销售量:%d\n",pEmp->SalesVolume);
printf("销售总额:%d\n",pEmp->GrossSales);
}

void Edit(Emp* pEmp)
{
int flag;
printf("choice class(1_manager,2_salesman or informal_salesman):\n");
scanf("%d",&flag);
if(flag == 1)
{
printf("请输入员工号:");
scanf("%d", &pEmp->EmpId);
printf("\n请输入姓名:");
scanf("%s", &pEmp->EmpName);
printf("\n请输入职务:manager");
printf("\n工资=10000RMB");
pEmp->Pay=10000;
strcpy(pEmp->Position, 'm');
}
if(flag == 2)
{
printf("请输入员工号:");
scanf("%d", &pEmp->EmpId);
printf("\n请输入姓名:");
scanf("%s", &pEmp->EmpName);
printf("\n请输入销售量:");
scanf("%d",&pEmp->SalesVolume);
printf("\n请输入销售总额:");
scanf("%d",&pEmp->GrossSales);
pEmp->Pay=pEmp->GrossSales/10;
printf("\n工资=%dRMB",pEmp->Pay);

if(pEmp->GrossSales >= 5000)
{
strcpy(pEmp->Position, 'fs');
}
else
{
strcpy(pEmp->Position, 'infs');
}
printf("\n职务:%s",pEmp->Position);
}
}

void inputEmpInfo()
{
Emp* pEmp = 0;
pEmp = AllocateEmp();

Edit(pEmp);
}

void showAll()
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (1 == EmpArray.usedFlag[i])
{
show(&EmpArray.employee[i]);
}
}

}

void deleteEmp()
{
int i;
int empNo = 0;

printf("\n请输入要删除的员工的员工号:");
scanf("%d", &empNo);

for (i = 0; i < 100; i++)
{
if (EmpArray.employee[i].EmpId == empNo)
{
EmpArray.usedFlag[i] = 0;
printf("删除成功!\n");
break;
}
}
}

Emp* findByEmpNo(int empNo)
{
int i;
for (i = 0; i < 100; i++)
{
if (EmpArray.employee[i].EmpId == empNo)
{
return &EmpArray.employee[i];
}
}
return 0;
}

void changeEmp()
{
Emp* pEmp;
int empNo = 0;

printf("\n请输入要修改的员工的员工号:");
scanf("%d", &empNo);


pEmp = findByEmpNo(empNo);
if (0 == pEmp)
{
printf("没有找到输入的员工号\n");
}
else
{
Edit(pEmp);
}
}



void saveall(Emp* pEmp)
{

fw=fopen("information.txt","w");
fprintf(fw,"\n-------------------------员工信息---------------------------\n");
fprintf(fw,"员工号:%d\n",pEmp->EmpId);
fprintf(fw,"姓名:%s\n", pEmp->EmpName);
fprintf(fw,"职务:%s\n",pEmp->Position);
fprintf(fw,"工资:%d\n",pEmp->Pay);
fprintf(fw,"销售量:%d\n",pEmp->SalesVolume);
fprintf(fw,"销售总额:%d\n",pEmp->GrossSales);
fclose(fw);
}

void saveEmp()
{

int i = 0;
for (i = 0; i < 100; i++)
{
if (1 == EmpArray.usedFlag[i])
{
saveall(&EmpArray.employee[i]);
}
}

}



void findByName()
{
char EmpName[200];
int i;
int findCount;

findCount = 0;

printf("\n请输入员工的姓名:");
scanf("%s", EmpName);

for (i = 0; i < 100; i++)
{
if (0 == strcmp(EmpArray.employee[i].EmpName, EmpName))
{
show(&EmpArray.employee[i]);
findCount++;
}
}

printf("\n一共找到%d个员工。\n",findCount);

}

void findByNumber()
{
int EmpId=0;
int i;
int findCount = 0;
printf("\n请输入员工的编号:");
scanf("%d", EmpId);
for (i = 0; i < 100; i++)
{
if (0 == strcmp(EmpArray.employee[i].EmpId,EmpId))
{
show(&EmpArray.employee[i]);
findCount++;
}
}
printf("\n一共找到%d个员工。\n",findCount);
}

void FindEmp()
{
char o[10];
menuFind();
scanf("%s", o);

switch(*o)
{
case 'a':
findByName();
break;
case 'b':
findByNumber();
break;
default:
break;
}
}

int main(int argc, char *argv[])
{
char op;

while(1)
{
menu();
scanf("%s", &op);
//op = getchar();

if ( ('I' == op) || ('i' == op))
{
inputEmpInfo();
}
else if ( ('D' == op) || ('d' == op))
{
deleteEmp();
}
else if ( ('C' == op) || ('c' == op))
{
changeEmp();
}
else if ( ('V' == op) || ('v' == op))
{
showAll();
}
else if ( ('F' == op) || ('f' == op))
{
FindEmp();
}
else if ( ('S' == op) || ('s' == op))
{
saveEmp();
}
else if ( ('Q' == op) || ('q' == op))
{
break;
}
else
{
printf("Invalidate operation!");
}

}
return 0;
}


...全文
170 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lastnemo 2011-04-16
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ulfsaar 的回复:]

引用 7 楼 lastnemo 的回复:
再次感谢你——Ulfsaar。多问一句,你很爱熊战?

一般吧,最爱屠夫
[/Quote]我挺喜欢后期输出的,技术含量高的打不好
Ulfsaar 2011-04-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lastnemo 的回复:]
再次感谢你——Ulfsaar。多问一句,你很爱熊战?
[/Quote]
一般吧,最爱屠夫

lastnemo 2011-04-15
  • 打赏
  • 举报
回复
再次感谢你——Ulfsaar。多问一句,你很爱熊战?
lastnemo 2011-04-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ulfsaar 的回复:]

引用 3 楼 lastnemo 的回复:
引用 1 楼 ulfsaar 的回复:

用编号查询的时候,两个int型的你怎么能用strcmp比较
if (0 == strcmp(EmpArray.employee[i].EmpName, EmpName))

不知道你那是什么编译器,这样都不报错
就是VC6.0啊,编译时能通过,但是在运行查看时,如果通过编号来查,就会出错,跳出那个运……
[/Quote]是1.exe - 0 error(s), 0 warning(s)。上网查了下确实只能用来比较字符串,不能比较数字。可能我用的是虚拟机的缘故吧。
lastnemo 2011-04-15
  • 打赏
  • 举报
回复
啊哈,我知道哪错了
scanf("%d", EmpId);scanf("%s", EmpName);都没加地址符号。
I Need more patience and care

Ulfsaar 2011-04-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lastnemo 的回复:]
引用 1 楼 ulfsaar 的回复:

用编号查询的时候,两个int型的你怎么能用strcmp比较
if (0 == strcmp(EmpArray.employee[i].EmpName, EmpName))

不知道你那是什么编译器,这样都不报错
就是VC6.0啊,编译时能通过,但是在运行查看时,如果通过编号来查,就会出错,跳出那个运到问题的对话框
[/Quote]

我也是用vc6.0啊,有报warning么?
你看看的警告级别吧(warning level),是不是比3低
lastnemo 2011-04-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ulfsaar 的回复:]

用编号查询的时候,两个int型的你怎么能用strcmp比较
if (0 == strcmp(EmpArray.employee[i].EmpName, EmpName))

不知道你那是什么编译器,这样都不报错
[/Quote]就是VC6.0啊,编译时能通过,但是在运行查看时,如果通过编号来查,就会出错,跳出那个运到问题的对话框
Ulfsaar 2011-04-15
  • 打赏
  • 举报
回复
把你的Code稍微改了下,在我这能Compile过,你试试看
在字符串比较的时候,要是不需要区分大小写,用stricmp

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

typedef struct Emp
{
int EmpId; // 员工编号
char EmpName[200]; // 姓名
char Position[200]; // 职务
int Pay; // 工资
int SalesVolume; // 销售量
int GrossSales; // 销售总额
}Emp;

struct
{
Emp employee[100];
int usedFlag[100]; // 这个标志为用于标志相同索引的 employee数组中的变量 是否被使用。 1 --- 使用了, 0 --- 未使用
int sortByWorkYears[100];
}EmpArray;

FILE *fw;


void menu()
{
printf("\n**************************MENU********************\n");
printf("V|v ---- 显示:\n");
printf("F|f ---- 查询:\n");
printf("I|i ---- 录入:\n");
printf("D|d ---- 删除:\n");
printf("C|c ---- 修改:\n");
printf("S|s ---- 保存:\n");
printf("Q|q ---- 退出程序\n");
printf("请选择操作的种类:\n");

}

void menuFind()
{
printf("a. 按姓名查找职工记录\n");
printf("b. 按编号查找职工记录\n");
printf(" 其他键返回上级菜单! \n");
printf("请选择操作的种类:\n");
}

Emp* AllocateEmp()
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (0 == EmpArray.usedFlag[i])
{
EmpArray.usedFlag[i] = 1;
return &EmpArray.employee[i];
}
}
return 0;
}


void show(Emp* pEmp)
{
printf("\n-------------------------员工信息---------------------------\n");
printf("员工号:%d\n",pEmp->EmpId);
printf("姓名:%s\n", pEmp->EmpName);
printf("职务:%s\n",pEmp->Position);
printf("工资:%d\n",pEmp->Pay);
printf("销售量:%d\n",pEmp->SalesVolume);
printf("销售总额:%d\n",pEmp->GrossSales);
}

void Edit(Emp* pEmp)
{
int flag;
printf("choice class(1_manager,2_salesman or informal_salesman):\n");
scanf("%d",&flag);
if(flag == 1)
{
printf("请输入员工号:");
scanf("%d", &pEmp->EmpId);
printf("\n请输入姓名:");
scanf("%s", &pEmp->EmpName);
printf("\n请输入职务:manager");
printf("\n工资=10000RMB");
pEmp->Pay=10000;
strcpy(pEmp->Position, "m");
}
if(flag == 2)
{
printf("请输入员工号:");
scanf("%d", &pEmp->EmpId);
printf("\n请输入姓名:");
scanf("%s", &pEmp->EmpName);
printf("\n请输入销售量:");
scanf("%d",&pEmp->SalesVolume);
printf("\n请输入销售总额:");
scanf("%d",&pEmp->GrossSales);
pEmp->Pay=pEmp->GrossSales/10;
printf("\n工资=%dRMB",pEmp->Pay);

if(pEmp->GrossSales >= 5000)
{
strcpy(pEmp->Position, "fs");
}
else
{
strcpy(pEmp->Position, "infs");
}
printf("\n职务:%s",pEmp->Position);
}
}

void inputEmpInfo()
{
Emp* pEmp = 0;
pEmp = AllocateEmp();

Edit(pEmp);
}

void showAll()
{
int i = 0;
for (i = 0; i < 100; i++)
{
if (1 == EmpArray.usedFlag[i])
{
show(&EmpArray.employee[i]);
}
}

}

void deleteEmp()
{
int i;
int empNo = 0;

printf("\n请输入要删除的员工的员工号:");
scanf("%d", &empNo);

for (i = 0; i < 100; i++)
{
if (EmpArray.employee[i].EmpId == empNo)
{
EmpArray.usedFlag[i] = 0;
printf("删除成功!\n");
break;
}
}
}

Emp* findByEmpNo(int empNo)
{
int i;
for (i = 0; i < 100; i++)
{
if (EmpArray.employee[i].EmpId == empNo)
{
return &EmpArray.employee[i];
}
}
return 0;
}

void changeEmp()
{
Emp* pEmp;
int empNo = 0;

printf("\n请输入要修改的员工的员工号:");
scanf("%d", &empNo);


pEmp = findByEmpNo(empNo);
if (0 == pEmp)
{
printf("没有找到输入的员工号\n");
}
else
{
Edit(pEmp);
}
}



void saveall(Emp* pEmp)
{

fw=fopen("information.txt","w");
fprintf(fw,"\n-------------------------员工信息---------------------------\n");
fprintf(fw,"员工号:%d\n",pEmp->EmpId);
fprintf(fw,"姓名:%s\n", pEmp->EmpName);
fprintf(fw,"职务:%s\n",pEmp->Position);
fprintf(fw,"工资:%d\n",pEmp->Pay);
fprintf(fw,"销售量:%d\n",pEmp->SalesVolume);
fprintf(fw,"销售总额:%d\n",pEmp->GrossSales);
fclose(fw);
}

void saveEmp()
{

int i = 0;
for (i = 0; i < 100; i++)
{
if (1 == EmpArray.usedFlag[i])
{
saveall(&EmpArray.employee[i]);
}
}

}



void findByName()
{
char EmpName[200];
int i;
int findCount;

findCount = 0;

printf("\n请输入员工的姓名:");
scanf("%s", EmpName);

for (i = 0; i < 100; i++)
{
if (0 == stricmp(EmpArray.employee[i].EmpName, EmpName))
{
show(&EmpArray.employee[i]);
findCount++;
}
}

printf("\n一共找到%d个员工。\n",findCount);

}

void findByNumber()
{
int EmpId=0;
int i;
int findCount = 0;
printf("\n请输入员工的编号:");
scanf("%d", EmpId);
for (i = 0; i < 100; i++)
{
if (EmpArray.employee[i].EmpId == EmpId)
{
show(&EmpArray.employee[i]);
findCount++;
}
}
printf("\n一共找到%d个员工。\n",findCount);
}

void FindEmp()
{
char o[10];
menuFind();
scanf("%s", o);

switch(*o)
{
case 'a':
findByName();
break;
case 'b':
findByNumber();
break;
default:
break;
}
}

int main(int argc, char *argv[])
{
char op;

while(1)
{
menu();
scanf("%s", &op);
//op = getchar();

if ( ('I' == op) || ('i' == op))
{
inputEmpInfo();
}
else if ( ('D' == op) || ('d' == op))
{
deleteEmp();
}
else if ( ('C' == op) || ('c' == op))
{
changeEmp();
}
else if ( ('V' == op) || ('v' == op))
{
showAll();
}
else if ( ('F' == op) || ('f' == op))
{
FindEmp();
}
else if ( ('S' == op) || ('s' == op))
{
saveEmp();
}
else if ( ('Q' == op) || ('q' == op))
{
break;
}
else
{
printf("Invalidate operation!");
}

}
return 0;
}

Ulfsaar 2011-04-15
  • 打赏
  • 举报
回复
用编号查询的时候,两个int型的你怎么能用strcmp比较
if (0 == strcmp(EmpArray.employee[i].EmpName, EmpName))

不知道你那是什么编译器,这样都不报错

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧