猿问

编程执行有问题


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int m, n;
struct student
{
int num;
char name[10];
char sex[5];
char class[10];
char major[10];
int dorm;
}stu[1];

void input()
{
int i = 0;
char t = 'y';
FILE *fp;
fp = fopen("inform.txt", "w");
if (fp = NULL)
{
printf("error!\n");
exit(0);
while (1)
{
printf("please enter student data:");
scanf("%d\n%d\n%s\n%c\n%s\n%s\n", &stu[i].num, &stu[i].dorm, &stu[i].name, &stu[i].sex, &stu[i].class,&stu[i].major);
printf("是否继续输入(y/n)?: ");
scanf("%c", &t);
if (t == 'y' || t == 'Y')
{
i++;
}
}
}
fclose(fp);
}

void add()
{
int i = 0;
char t = 'y';
FILE *fp; fp = fopen("inform.txt", "w");
if (fp = NULL)
{
printf("error!\n");
exit(0);
while (1)
{
printf("please enter student data:");
scanf("%d\n%d\n%s\n%c\n%s\n%s\n", &stu[i].num, &stu[i].dorm, &stu[i].name, &stu[i].sex, &stu[i].class,&stu[i].major);
printf("是否继续添加(y/n)?: ");
scanf("%c", &t);
if (t == 'y' || t == 'Y')
{
i++;
}
}
}
fclose(fp);
}

void del()
{
int i, num;
char t = 'y';
FILE *fp, *fp1;
printf("please enter del num:");
scanf("%d", &num);
printf("%d\n%d\n%s\n%c\n%s\n%s\n", &stu[i].num, &stu[i].dorm, &stu[i].name, &stu[i].sex, &stu[i].class,&stu[i].major);
for (n = i; n<m; n++)
{
stu[n] = stu[n + 1];
}
printf("OK");
printf("是否继续删除(y/n)?: ");
scanf("%c", &t);
if (t == 'y' || t == 'Y')
{
i++;
}
fclose(fp);
}

void change()
{
FILE *fp;
int num, i = 0;
char t = 'y';
fp = fopen("inform.txt", "rb+");
while (t == 'y' || t == 'Y')
{
while (1)
{
printf("请输入修改的学号:");
scanf("%d", &num);
if (num > 0)
break;
}
while (!feof(fp))
{
i = 0;
fread(&stu[i], (long)sizeof(struct student), 1, fp);
if (stu[i].num == num&&i == 0)
{
printf("你要修改的学生的信息:\n");
printf("%d\n%d\n%s\n%c\n%s\n%s\n", &stu[i].num, &stu[i].dorm, &stu[i].name, &stu[i].sex, &stu[i].class,&stu[i].major);
i++;
printf("你要修改的学生的信息:\n");
printf("please enter student data:");
scanf("%d\n%d\n%s\n%c\n%s\n%s\n", &stu[i].num, &stu[i].dorm, &stu[i].name, &stu[i].sex, &stu[i].class,&stu[i].major);
fseek(fp, -(long)sizeof(struct student), 1);
fwrite(&stu[i], (long)sizeof(struct student), 1, fp);
fseek(fp, (long)sizeof(struct student), 1);
}
}
printf("是否继续修改(y/n):");
scanf("%c", &t);
if (t == 'y' || t == 'Y')
{
rewind(fp); i = 0;
}
}
fclose(fp);
}

void search()
{
FILE *fp;
int num,i=0;
char t = 'y';
fp = fopen("inform.dat", "rb");
while (t == 'y' || t == 'Y')
{
while (1)
{
printf("请输入查询的学号:");
scanf("%d", &num);
if (num > 0)
break;
}
while (!feof(fp))
{
fread(&stu[i], (long)sizeof(struct student), 1, fp);
if (stu[i].num == num)
{
printf("%d\n%d\n%s\n%c\n%s\n%s\n", &stu[i].num, &stu[i].dorm, &stu[i].name, &stu[i].sex, &stu[i].class,&stu[i].major);
break;
}
}
printf("是否要继续查找(y/n):");
scanf("%c", &t);
if (t == 'y' || t == 'Y')
{
rewind(fp);
}
fclose(fp);
}
}

void off()
{
exit(0);
}

void main()
{
int ch;
do
{
printf("         欢迎进入高校学生宿舍学生信息管理系统            \n");
printf("                                                          \n");
printf("                                                          \n");
printf("             请选择操作内容:                             \n");
printf("1输入                                                     \n");
printf("2添加                                                     \n");
printf("3删除                                                     \n");
printf("4修改                                                     \n");
printf("5查询                                                     \n");
printf("6退出                                                     \n");
printf("                                                          \n");
printf("please choice:(0-6):");
scanf("%d", &ch);
switch (ch)
{
case 1:input(); break;
case 2:add(); break;
case 3:del(); break;
case 4:change(); break;
case 5:search(); break;
case 6:off(); break;
default:break;
}
} while (1);
}

慕雪6341044
浏览 1298回答 2
2回答

qq_莫非

说明代码里面还是有错
随时随地看视频慕课网APP
我要回答