|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<string.h>
main()
{
int i;
char input[10];
struct Student
{
char num[20];
char name[20];
char sex[10];
int score;
};
struct Student student1={"MZ441426","Mike","Male",516};
printf("该考生编号为:%s\n",student1.num);
printf("请问您要查询什么?\n(输入name查询姓名,输入sex查询性别,输入score查询成绩,输入No或no停止查询。)\n");
while(1)
{
scanf("%s",input);
for (i=0;i>=0;i++)
{
if (strcmp(input,"name")==0)
{printf("该考生姓名为%s\n接下来查询?\n",student1.name);break;}
if (strcmp(input,"sex")==0)
{printf("该考生性别为%s\n接下来查询?\n",student1.sex);break;}
if(strcmp(input,"score")==0)
{printf("该考生成绩为%d\n接下来查询?\n",student1.score);break;}
}
if(strcmp(input,"No")==0||strcmp(input,"no")==0)
{printf("感谢您的使用!\n");break;}
}
}
代码如上,是正确的,但是当我输入no时,要挺久才能打印出感谢您的使用这句话,请问是为啥,我用了两个编译器都是这样
等的时间长,问题出在for循环上了
for (i=0;i>=0;i++)i,这个for循环,i初值为0,若输入的不是姓名、性别、成绩,那么就不会遇到break,i会从0一直加到溢出变为负数,才跳出循环。完全可以不要这个for循环
- while(1)
- {
- scanf("%s",input);
-
- if (strcmp(input,"name")==0)
- printf("该考生姓名为%s\n接下来查询?\n",student1.name);
- else if (strcmp(input,"sex")==0)
- printf("该考生性别为%s\n接下来查询?\n",student1.sex);
- else if(strcmp(input,"score")==0)
- printf("该考生成绩为%d\n接下来查询?\n",student1.score);
- else if(strcmp(input,"No")==0||strcmp(input,"no")==0)
- {
- printf("感谢您的使用!\n");
- break;
- }
- }
复制代码
|
|