学生管理系统 宏 出错 名字查找很诡异 scanf() getchar() 用法


今天做了一个C语言简单的学生管理系统,发现一个很奇怪的现象,学疏才浅,小问题都解决不了,先记录下来吧,后面再解决 再更新。

错误:

1,当使用了#denfine N 256 的时候就会出现很多的错误;

2,用名字查找的时候出现错误;

3,还不是很清楚scanf 和 getchar 这两个函数的相关机制;


代码:

#include <stdio.h>
#include <stdlib.h>
#define S 256;
char ch;
int i=0;

struct students
{
long Number;
char Name[256];
int Chinese;
int Math;
int English;
char Info[256];
}stu[256];


int main(int argc, char *argv[])
{
void READ(void);
void SAVE(void);

void search_number(int i,int number);
void search_name(int i,char *name);


void Seeall(int count);
void show(int k);
int num;

READ();

printf("STUDENT MANAGMENT INFORMATION SYSTEM\n");
printf("1:Add the student's information\n");
printf("2:Search the student's information\n");
printf("3:Delete the student's information\n");
printf("4: Look all of the information\n");
printf("5:exit\n");
printf("\n");


printf("Please enter the number you want to choice(1,2,3,4 or 5):");

scanf("%d",&num);

while(num!=1&&num!=2&&num!=3&&num!=4&&num!=5)
{
printf("ERROR!Please enter your number again…:");
scanf("%d",&num);
}

do
{
switch(num)
{
case 1:
{
printf("\nEnter tht information of the student %dth !\n",i+1);

printf("Enter the number:");
scanf("%ld",&stu[i].Number);
getchar();

printf("Enter the name:");
scanf("%s",&stu[i].Name);
getchar();

printf("Enter the Chinese score:");
scanf("%d",&stu[i].Chinese);
getchar();

printf("Enter the math score:");
scanf("%d",&stu[i].Math);
getchar();

printf("Enter the English score:");
scanf("%d",&stu[i].English);
getchar();

printf("Please enter the other informations:");
scanf("%s",stu[i].Info);

SAVE();

i++;
}
break;

case 2:
{
do
{
int str_search;
printf("Please enter your choice,1:Number;2:Name:");
scanf("%d",&str_search);
if(str_search==1)
{
int temp_number;
printf("Please enter the number:");
scanf("%d",&temp_number);
getchar();
search_number(i,temp_number);
}
else if(str_search==2)
{
char *temp_name;
temp_name=(char *)malloc(256);


printf("Please enter the name:");
scanf("%s",temp_name);
search_name(i,temp_name);
free(temp_name);
}
printf("ENTER to continue,others to MENUE");
ch=getchar();
}while(ch=='\n');
}
break;

case 3:
{
do
{
int k;
int Del_number;
char *del;
del=(char *)malloc(256);
printf("\nPlease enter the number and the name of the student you want to delete:");
scanf("%d %s",&Del_number,del);
getchar();
search_number(i,Del_number);
printf("Are you sure to delete the student?1:YES 2:NO");


scanf("%d",&k);

if(k==1)
{
int m;
int j;
for(m=0;m<i;m++)
{
if(Del_number==stu[m].Number) break;
}

for(j=m;j<i;j++)
{
stu[m]=stu[m+1];
}
i--;
printf("SUCCEED!\n");
}
printf("\nENTER to continue,others to CHOISE");
ch=getchar();


}while(ch=='\n');

}
break;

case 4:
{
Seeall(i);
}
break;


case 5:
{
SAVE();
exit(0);
}
break;


default:break;
}

ch=getchar();
printf("\nPlease enter your choice,1,2,3,4 or 5:");
scanf("%d",&num);
getchar();
}while(ch=='\n');


SAVE();
return 0;

}


void SAVE(void)
{
int temp;
FILE *fp;
fp=fopen("C:\\Student.dat","wb");
if(fp==NULL)
{
printf("Data save fail\n");
}
else
{
fwrite(&i,sizeof(int),1,fp);
for(temp=0;temp<i;temp++)
{
fwrite(&stu[temp].Number,sizeof(stu[temp].Number),1,fp);
fwrite(&stu[temp].Name,sizeof(stu[temp].Name),1,fp);
fwrite(&stu[temp].Chinese,sizeof(stu[temp].Chinese),1,fp);
fwrite(&stu[temp].Math,sizeof(stu[temp].Math),1,fp);
fwrite(&stu[temp].English,sizeof(stu[temp].English),1,fp);
fwrite(&stu[temp].Info,sizeof(stu[temp].Info),1,fp);
}
}
fclose(fp);
}


void READ(void)
{
int temp;
FILE *fp;
fp=fopen("C:\\Student.dat","rb");
if(fp==NULL)
{
printf("Open fail\n");

}
else
{
fread(&i,sizeof(int),1,fp);

for(temp=0;temp<i;temp++)
{
fread(&stu[temp].Number,sizeof(stu[temp].Number),1,fp);
fread(&stu[temp].Name,sizeof(stu[temp].Name),1,fp);
fread(&stu[temp].Chinese,sizeof(stu[temp].Chinese),1,fp);
fread(&stu[temp].Math,sizeof(stu[temp].Math),1,fp);
fread(&stu[temp].English,sizeof(stu[temp].English),1,fp);
fread(&stu[temp].Info,sizeof(stu[temp].Info),1,fp);
}
}
fclose(fp);
}


void search_number(int i,int number)
{
int k;
int flag=0;
for(k=0;k<i;k++)
{
if(number==stu[k].Number)
{
show(k);
flag=1;
}
}


if(flag==0) printf("Do not fine the student you want");

}


void search_name(int i,char *name)
{
int flag=0;
int k;
for(k=0;k<i;k++)
{
if(strcmp(stu[k].Name,name)==0);
{
show(k);
flag=1;
}
}
if(flag==0) printf("Do not fine the student you want");
}


void Seeall(int count)
{
int k;
for(k=0;k<count;k++)
{
show(k);
}
}


void show(int k)
{
printf("\nSUCEED\n");
printf("Number:%d   Name:%s\n",stu[k].Number,stu[k].Name);
printf("Chinese:%d   Math%d   English%d\n",stu[k].Chinese,stu[k].Math,stu[k].English);
printf("Infomation:%s\n",stu[k].Info);
printf("\n");
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值