瑟瑟发抖的萌新,我 C 语言程序哪里错了。。。signal SIGSEGV,Segmentation fault

2016-12-18 17:59:22 +08:00
 998998

#include <stdio.h> #include <stdlib.h> #include <string.h> struct worker { char name[30]; int ID; char position[15]; int salary1,salary2,salary3,salary4,salary5,salary6; }wo[100];
void input(int ,int); void dis(int); void search(int);
void del(int );
void modify(int); void author(); void add(int ,int); void fengmian(); main() { int i; int M=0; int count=0; fengmian(); for(;;) { system("cls"); printf("\n"); printf("\t\t
**********************************************\n"); printf("\t\t* menue \n");
printf("\t\t
[1] input \n"); printf("\t\t [2] display \n"); printf("\t\t [3] search \n"); printf("\t\t [4] delete \n"); printf("\t\t [5] modify \n"); printf("\t\t [6] author \n"); printf("\t\t [7] add \n"); printf("\t\t [0] exit \n"); printf("\t\t************************************************\n"); printf("\t\t\tplease select 0-7:"); scanf("%d",&i); system("cls"); switch(i) {
case 1: input(&count,M);break; case 2: dis(count);break; case 3: search(count);break; case 4: del(&count);break; case 5: modify(count);break; case 6: author();break; case 7: add(&count,M);break; case 0: printf("\t\t\tthank you for use!\n");system("pause");exit(0);break; default: printf("\t\t\twrong!\n");system("pause");break; }
getchar(); system("cls"); } }

void input(int *count,int M) { int numberTemp,i,j;int ID; if(*count>=100){printf("No room exist!\n");}
printf("\tplease input number of workers to enter:"); scanf("%d",&numberTemp); for(i=0;i<numberTemp;i++) { system("cls"); printf("The NO.%d worker\n",i+1); printf("ID:");scanf("%d",&ID); for(j=0;j<*count;j++) if(wo[j].ID==ID) {printf("There is the same ID!\n");return;} wo[i].ID=ID; printf("name:"); scanf("%s",wo[i].name); printf("position:");scanf("%s",wo[i].position); printf("jbgz:"); scanf("%d",&wo[i].salary1); printf("gwjt:"); scanf("%d",&wo[i].salary2); printf("qtbt:"); scanf("%d",&wo[i].salary3); printf("kcbf:"); scanf("%d",&wo[i].salary4); wo[i].salary5=wo[i].salary1+wo[i].salary2+wo[i].salary3; wo[i].salary6=wo[i].salary5-wo[i].salary4; (*count)++;M++; printf("\t\tsuccess\n"); getchar(); } }

void add(int *count,int M) { int numberTemp,i,j;int ID;int p=M+1; if(*count>=100){printf("No room exist!\n");}
printf("\tplease input number of workers to enter:"); scanf("%d",&numberTemp); for(i=p;i<p+numberTemp;i++) { system("cls"); printf("The NO.%d worker\n",i+1); printf("ID:");scanf("%d",&ID); for(j=0;j<*count;j++) if(ID==wo[j].ID) {printf("There is the same ID!\n");return;} wo[i].ID=ID; printf("name:"); scanf("%s",wo[i].name); printf("position:");scanf("%s",wo[i].position); printf("jbgz:"); scanf("%d",&wo[i].salary1); printf("gwjt:"); scanf("%d",&wo[i].salary2); printf("qtbt:"); scanf("%d",&wo[i].salary3); printf("kcbf:"); scanf("%d",&wo[i].salary4); wo[i].salary5=wo[i].salary1+wo[i].salary2+wo[i].salary3; wo[i].salary6=wo[i].salary5-wo[i].salary4; (*count)++;M++; printf("\t\tsuccess\n"); getchar(); } }

void dis(int count) { int i; printf("\t\tThe following is all information\n"); printf("name\tId\tpos\tjbgz\tgwbt\tqtbt\tkcbf\tyfgz\tsfgz\n"); for(i=0;i<count;i++) { printf("%s\t%d\t%s\t%d\t%d\t%d\t%d\t%d\t%d\n",wo[i].name,wo[i].ID, wo[i].position,wo[i].salary1,wo[i].salary2,wo[i].salary3,wo[i].salary4, wo[i].salary5,wo[i].salary6);
} printf("\t\tinput enter to back\n"); getchar(); }

void search(int count) { int n,i,flag=1;int id;char name[30]; printf("\t\t1 search by name\n"); printf("\t\t2 search by ID\n"); printf("\t\tplease select 1-2:"); scanf("%d",&n); switch(n) { case 1: printf("\t\tplease input name:"); scanf("%s",name); i=0; printf("name\tId\tpos\tjbgz\tgwbt\tqtbt\tkcbf\tyfgz\tsfgz\n"); do{ if (strcmp(name,wo[i].name)==0) { printf("%s\t%d\t%s\t%d\t%d\t%d\t%d\t%d\t%d\n",wo[i].name,wo[i].ID, wo[i].salary1,wo[i].salary2,wo[i].salary3,wo[i].salary4, wo[i].salary5,wo[i].salary6); flag=0; } }while(i<count); if(flag==1) printf("\t\tnot found\n"); break; case 2: printf("\t\tplease input ID:"); scanf("%d",&id);i=0; printf("name\tId\tpos\tjbgz\tgwbt\tqtbt\tkcbf\tyfgz\tsfgz\n"); do { if (id==wo[i].ID) { printf("%s\t%d\t%s\t%d\t%d\t%d\t%d\t%d\t%d\n",wo[i].name,wo[i].ID, wo[i].salary1,wo[i].salary2,wo[i].salary3,wo[i].salary4, wo[i].salary5,wo[i].salary6); flag=0; } }while(i<count); if(flag==1) printf("\t\tnot found!\n"); break; default: printf("\t\twrong!\n"); break; } printf("\t\tinput enter to back\n"); }

void del(int *count) { int id; int i,j,flag=1; printf("\t\tplease input ID:"); scanf("%d",&id); for(i=0;i<*count&&flag;i++) {if(id==wo[i].ID) {for(j=i;j<*count-1;j++) wo[j]=wo[j+1]; flag=0; (*count)--; } else
printf("\t\tnot found!"); }

}

void modify(int count) { int i,n;int id; printf("\t\tplease input the ID of worker to modify:"); scanf("%d",&id); printf("name\tId\tpos\tjbgz\tgwbt\tqtbt\tkcbf\tyfgz\tsfgz\n"); for(i=0;i<count;i++) if(id==wo[i].ID) {
printf("%s\t%d\t%s\t%d\t%d\t%d\t%d\t%d\t%d\n",wo[i].name,wo[i].ID, wo[i].salary1,wo[i].salary2,wo[i].salary3,wo[i].salary4, wo[i].salary5,wo[i].salary6); for(;;) { printf("\n"); printf("\t\t*************************************************\n"); printf("\t\t* menue \n");
printf("\t\t
[1] name \n"); printf("\t\t [2] position \n"); printf("\t\t [3] jbgz \n"); printf("\t\t [4] gwjt \n"); printf("\t\t [5] qtbt \n"); printf("\t\t [6] kcbf \n"); printf("\t\t [0] exit \n"); printf("\t\t************************************************\n"); printf("\t\t\tplease select 0-6:"); scanf("%d",&n); system("cls"); switch(n) {
case 1: printf("\tnew name:");scanf("%s",wo[i].name);break; case 2: printf("\tnew position:");scanf("%s",wo[i].position);break; case 3: printf("\tnew jbgz:");scanf("%d",&wo[i].salary1); wo[i].salary5=wo[i].salary1+wo[i].salary2+wo[i].salary3; wo[i].salary6=wo[i].salary5-wo[i].salary4;break; case 4: printf("\tnew gwjt:");scanf("%d",&wo[i].salary2); wo[i].salary5=wo[i].salary1+wo[i].salary2+wo[i].salary3; wo[i].salary6=wo[i].salary5-wo[i].salary4;break; case 5: printf("\tnew qtbt:");scanf("%d",&wo[i].salary3); wo[i].salary5=wo[i].salary1+wo[i].salary2+wo[i].salary3; wo[i].salary6=wo[i].salary5-wo[i].salary4;break; case 6: printf("\tnew kcxm:");scanf("%d",&wo[i].salary4); wo[i].salary5=wo[i].salary1+wo[i].salary2+wo[i].salary3; wo[i].salary6=wo[i].salary5-wo[i].salary4;break; case 0: exit(0);break; default: printf("\t\t\twrong!\n");system("pause");break; } getchar(); system("cls"); } } }

void author() { printf("\t\t**************************************\n\n"); printf("\t\t* \n\n"); printf("\t\t \n\n"); printf("\t\t \n\n"); printf("\t\t \n\n"); printf("\t\t \n\n"); printf("\t\t*************************************\n\n"); system("pause"); }

void fengmian() {
printf("\n\n\n"); printf("\t welcome to use the worker wage management system\n"); printf("\n\n\n"); printf("\t\t\tpress enter to continue\n"); getchar(); }

2790 次点击
所在节点    C
14 条回复
jimzhong
2016-12-18 18:16:04 +08:00
你贴到 gist 上把。。。。
SIGSEGV 一般是内存访问越界
bdbai
2016-12-18 18:18:53 +08:00
用 Markdown 排下版呗

```c
// 代码
```
luxinxin
2016-12-18 18:38:21 +08:00
进入调试模式跑一下看看?
UnknownR
2016-12-18 18:42:58 +08:00
看着蛋疼,可以排下版, v2 是支持 markdown 语法的,或者直接截 ide 的图也行
langjiyuan
2016-12-18 18:49:42 +08:00
(◎﹏◎)眼花。。排一下版啊。。
wevsty
2016-12-18 19:04:03 +08:00
首先这代码编译就应该没办法过才对
楼主声明的函数是这样的
void input(int, int);
而定义又是
void input(int *count, int M)

int 和 int *能一样?编译器没有给 error 么?

我不知道楼主怎么使用以后会出 SIGSEGV ,光贴个代码有啥意义?我怎么知道你做了些什么操作导致程序有什么问题?
举个例子
scanf("%s", wo[i].name);
看起来没问题楼主定义的 struct 中是这样定义的
char name[30];
可是如果用户输入超过 30 个字符会怎么样?
安全编码第一条,永远不要相信来自用户的输入
chiu
2016-12-18 19:11:09 +08:00
有谁这样看完了的!?
998998
2016-12-18 19:17:20 +08:00
@wevsty sorry ,我在复制时候出了问题,而且。。。我在自学 C 语言,这只是一个实验的程序。。。
zjbztianya
2016-12-18 20:00:12 +08:00
用 gdb 吧 神器
owt5008137
2016-12-19 09:31:33 +08:00
代码贴成这样,有人会看么
bluefalconjun
2016-12-19 09:47:25 +08:00
请先阅读一下 提问的智慧...
https://lug.ustc.edu.cn/wiki/doc/smart-questions
bombless
2016-12-19 11:24:45 +08:00
http://paste.ubuntu.com/23651556/
C:\projects>cl v.c
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24210 for x64
Copyright (C) Microsoft Corporation. All rights reserved.

v.c
v.c(199): warning C4477: 'printf' : format string '%s' requires an argument of type 'char *', but variadic argument 3 has type 'int'
v.c(199): warning C4313: 'printf': '%s' in format string conflicts with argument 3 of type 'int'
v.c(199): warning C4473: 'printf' : not enough arguments passed for format string
v.c(199): note: placeholders and their parameters expect 9 variadic arguments, but 8 were provided
v.c(199): note: the missing variadic argument 9 is required by format string '%d'
v.c(215): warning C4477: 'printf' : format string '%s' requires an argument of type 'char *', but variadic argument 3 has type 'int'
v.c(215): warning C4313: 'printf': '%s' in format string conflicts with argument 3 of type 'int'
v.c(215): warning C4473: 'printf' : not enough arguments passed for format string
v.c(215): note: placeholders and their parameters expect 9 variadic arguments, but 8 were provided
v.c(215): note: the missing variadic argument 9 is required by format string '%d'
v.c(259): warning C4477: 'printf' : format string '%s' requires an argument of type 'char *', but variadic argument 3 has type 'int'
v.c(259): warning C4313: 'printf': '%s' in format string conflicts with argument 3 of type 'int'
v.c(259): warning C4473: 'printf' : not enough arguments passed for format string
v.c(259): note: placeholders and their parameters expect 9 variadic arguments, but 8 were provided
v.c(259): note: the missing variadic argument 9 is required by format string '%d'
Microsoft (R) Incremental Linker Version 14.00.24210.0
Copyright (C) Microsoft Corporation. All rights reserved.

/out:v.exe
v.obj

编译器输出挺有帮助的, 233
lcsoft
2016-12-19 11:42:13 +08:00
贴出的代码格式很乱, LZ 就打算一直让它就这样乱着了?
loff
2016-12-19 15:29:52 +08:00
这代码看得我瑟瑟发抖

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/328471

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX