紧急的C语言问题

来源:百度知道 编辑:UC知道 时间:2024/06/03 14:18:58
当输入fun1()中的scanf语句时,没有输入完成就陷入死循环,是什么问题呢?
#include "stdio.h"
#include "string.h"
int M=2;
struct S1
{
char no[9];
char name[20];
char sex;
int age;
int score[4];
float sum;
float average;
}STUDENT[1000];
void fun0();
void fun1();
void fun2();
void fun3();
void fun4();
void fun5();
void fun6();
void fun7();
int A=0;
void main()
{
fun0();
}
void fun0()
{
int n,m;
do
{
m=0;
printf("Please enter a number ranging from 1 to 7 to choose the function\n");
printf("1 is Enter list\n");
printf("2 is Printf list\n");
printf("3 is Insert record from list\n");
printf("4 is Delete a record from list\n");
printf("5 is Search

不明白你在fun1()里调用 fun0()干嘛,你这不死循环吗? 

---------------------------------------------------

我也是菜鸟,你的程序我调试了好久,问题有点麻烦,总结下有两个地方:

1.结构体输入输出的问题。

printf("please input the sex:" );

scanf("%c",&STUDENT[i].sex);

printf("please input the age:");

scanf("%d",&STUDENT[i].age);

for(j=0;j<4;j++)

 scanf("%d",&STUDENT[i].score[j]);

STUDENT[i].sum=STUDENT[i].score[0]+STUDENT[i].score[1]+STUDENT[i].score[2]+STUDENT[i].score[3];

STUDENT[i].average=STUDENT[i].sum/4;

getch();

这一段代码在运行时得不到正确结果,原因在于在输入数据时,回车键也作为数据输入了,因此sex的内容存放的实际上是回车,你将其%d输出就能看出来,其值是10,对应的ASCII码就是回车。同样,age与score也因为回车的原因得不到正确值。

2.死循环。陷入死循环的原因在于default之后,假设需要输入n值我们输入一个a,则按程序,switch后执行default之后的语句,即  

default:printf("error\n");m=1;break;

break跳出该次循环直接执行下一次,因此while(m==1)此时实际上是没有执行,重新从do开始执行。在输出一段提示性语句后,又到了switch这里,但注意,