学生档案管理问题
这个程序是关于学生档案管理的,有两个功能,查找和修改,这个程序运行没有问题,但是我运行后不能进行修改功能,请高手帮忙下!!
#include <stdio.h>
struct student
{int num;
char name[10];
char sex;
char address[20];
};
struct student stu[3]={ {000,"li lin",'m',"103 beijing road"},
{001,"zhang fun",'m',"130 shanghai road"},
{002,"wang min",'f',"1010 zhongshan road"} } ;
struct student (*p)[3];
void main()
{
void query();
void modify();
char X,Z;
int i;
printf("num name sex address\n");
for (i=0;i<3;i++)
printf("%5d%10s%3c%20s\n",stu[i].num,stu[i].name,stu[i].sex,stu[i].address);
printf("do you want to query?Y OR N\n ");
scanf("%c",&X);
if (X=='Y'||X=='y')
query();
else if (X=='N'||X=='n')
printf("byebye!\n");
printf("do you want to modify?Y or N\n");
scanf("%c",&Z);
if (Z=='Y'||Z=='y')
modify();
else if(Z=='N'||Z=='n')
printf("byebye\n");
}
void query()
{
int i,number;
char Z;
printf("please enter numbers:");
scanf("%d",&number);
printf("num name sex address\n");
for (i=0;i<3;i++ )
{
if (number==stu[i].num)
printf("%3d%20s%2c%20s\n",stu[i].num,stu[i].name,stu[i].sex,stu[i].address);
}
}
void modify()
{
int modinum,k,i;
printf("please input theseat which you will modify:");
scanf("%d",&modinum);
for (i=0;i<3 ;i++ )
{if (modinum==stu[i].num)
{ k=i;
scanf("%d ",&stu[k].num);
scanf("%s",&stu[k].name);
scanf("%c",&stu[k].sex);
scanf("%s",&stu[k].address);}
}
}
附图,在图中的地方就有问题了 !!!
----------------解决方案--------------------------------------------------------
主要问题:
scanf("%d ",&stu[k].num);
scanf("%s",&stu[k].name); // 改为 stu[k].name
scanf("%c",&stu[k].sex);
scanf("%s",&stu[k].address); // 改为 stu[k].address
----------------解决方案--------------------------------------------------------
/* 一个修改版本 */
#include <stdio.h>
struct student
{
int num;
char name[10];
char sex;
char address[20];
};
struct student stu[3] = {
{000, "li lin", 'm', "103 beijing road"},
{001, "zhang fun", 'm', "130 shanghai road"},
{002, "wang min", 'f', "1010 zhongshan road"}
};
void query(void);
void modify(void);
int main(void)
{
char ch;
int i;
printf("num name sex address\n");
for (i = 0; i < 3; i++)
printf("%5d%10s%3c%20s\n", stu[i].num, stu[i].name,
stu[i].sex, stu[i].address);
putchar('\n');
printf("do you want to query? [Y/N]\n");
scanf("%c", &ch);
putchar('\n');
if(ch != '\n')
while(getchar() != '\n')
continue;
if (ch == 'Y' || ch == 'y')
query();
printf("do you want to modify? [Y/N]\n");
scanf("%c", &ch);
putchar('\n');
if(ch != '\n')
while(getchar() != '\n')
continue;
if (ch == 'Y' || ch == 'y')
modify();
printf("Bye!\n");
getchar();
return 0;
}
void query()
{
int i, number, k;
printf("please enter numbers: ");
while(scanf("%d", &number) != 1)
{
printf("please enter numbers[integer]: ");
while(getchar() != '\n')
continue;
}
while(getchar() != '\n')
continue;
printf("num name sex address\n");
for (i = k = 0; i < 3; i++)
{
if (number == stu[i].num)
{
printf("%-3d %-20s %c %20s\n", stu[i].num, stu[i].name,
stu[i].sex, stu[i].address);
k = 1;
}
}
if(k == 0)
printf("Not!\n");
putchar('\n');
}
void modify()
{
int modinum, k, i;
printf("please input theseat which you will modify:");
while(scanf("%d", &modinum) != 1)
{
printf("please input theseat which you will modify[integer]:");
while(getchar() != '\n')
continue;
}
while(getchar() != '\n')
continue;
for (i = k = 0; i < 3; i++ )
{
if (modinum == stu[i].num)
{
printf("Please enter num: ");
while(scanf("%d", &stu[i].num) != 1)
{
printf("Please enter num[integer]: ");
while(getchar() != '\n')
continue;
}
while(getchar() != '\n')
continue;
printf("Please enter name: ");
while(scanf("%s", stu[i].name) == 0)
{
while(getchar() != '\n')
continue;
printf("Please enter name: ");
}
while(getchar() != '\n')
continue;
do
{
printf("Please enter sex[m/f]: ");
scanf("%c", &stu[i].sex);
while(getchar() != '\n')
continue;
}while(stu[i].sex != 'm' && stu[i].sex != 'f');
printf("Please enter address: ");
while(scanf("%s", stu[i].address) == 0)
{
printf("Please enter address: ");
while(getchar() != '\n')
continue;
}
while(getchar() != '\n')
continue;
k = 1;
}
}
if(k == 0)
printf("Not!\n");
putchar('\n');
}
----------------解决方案--------------------------------------------------------
感谢
非常感谢这位高手!!! ----------------解决方案--------------------------------------------------------
大家说的都没错,当时我也觉得奇怪,就把输入之后的Z和X 输出看了以下,发现第二个字符输出的字符的ascii是10,而且换行的ascii是10,所以我觉得应该是换行的ascii赋给了Z,这样以来就明白了,回车其实占了两个字节的长度包括回车(13)和换行(10),当接收到(13)的时候把前面输入的给了第一个变量,然后自然换行(10)就给了第二个变量,而换行就只输出一个换行,都没注意到,一位没执行!要向避免这个的话也好办,向楼上大家说的那样,我觉得简单的办法是在两个输入之间加入一个getchar()这样,换行就会被getchar()接受,在输入也不会有问题,这个方法我亲自试了的,绝对没有问题!谢谢你提出了这么个有价值的问题供 大家研究
----------------解决方案--------------------------------------------------------
getchar() 返回的不可能是 \r ( 13 )。
我是为了去掉缓冲区中多余的字符,并且保证scanf() 可以读取下次的输入。
[[italic] 本帖最后由 cosdos 于 2008-1-3 19:09 编辑 [/italic]]
----------------解决方案--------------------------------------------------------
----------------解决方案--------------------------------------------------------