求助 关于if else以及循环问题
录入一定数据并进行查找,如不匹配则else单纯循环,无论输入值是否正确都只输出else语句内容代码如下:
#include<stdio.h>
struct personnel{
char name[15],birthplace[15];
int age, id;
//if the input ID is not int, the next two line will appear together//
//what is the problem?//
//Answer: the later Enter in the buffer makes the gets() skip//
//add flushall() will do good//
};
int ctr,check;
main()
{
struct personnel profile[3];
for(ctr=0; ctr<2; ctr++)
{
printf("\n%d.Hello, what's your name?\n", ctr+1);
gets(profile[ctr].name);
puts("\nwhat's your id number?");
scanf(" %d", &profile[ctr].id);
fflush(stdin);
puts("\nOk, what's your birthplace?");
gets(profile[ctr].birthplace);
fflush(stdin);
puts("\nAlright, what's your age");
scanf(" %d", &profile[ctr].age);
fflush(stdin);
}
puts("Which ID do you want to check:");
scanf(" %d", &check);
for(ctr=0; ctr<2; ctr++)
{if(check == profile[ctr].id)
{
printf("\nThe id you are checking is %d, name:%s, age:%d, born:%s\n\n",
profile[ctr].id, profile[ctr].name, profile[ctr].age,
profile[ctr].birthplace);
break;
}
else
{
puts("Please type in correct id number");
fflush(stdin);
scanf(" %d", &check);
continue;
}
}
}