求高手帮我看看,这个c语言程序哪里有错误??!!

2025-01-02 22:05:15
推荐回答(3个)
回答1:

#include "stdio.h"
#include "string.h"
struct aa
{ char name[10];
char tel[20];
};
void main()
{
int flag;
struct aa stud[]={{"Liming","88776655"},{"Wangping","88665544"},{"zhanghua","88554433"}};
char nn[10];
int i;
gets(nn);
for(i=0; i<3; i++)
if(strcmp(stud[i].name, nn)==0) flag=1;
{if(flag==1)printf("name:%s tel:%s\n", stud[i].name, stud[i].tel);
else printf("Can not find..\n");}
}

回答2:

修改后:
#include "stdio.h"
#include "string.h"

struct aa
{ char name[10];
char tel[20];
};
void main()
{
struct aa stud[]={{"Liming","88776655"},{"Wangping","88665544"},{"zhanghua","88554433"}};
char nn[10];
int i,flag = 0;

gets(nn);
for(i=0; i<3; i++)
{
if(strcmp(stud[i].name, nn)==0)
{
flag=1;
break;
}
}
if(flag==1)
printf("name:%s tel:%s\n", stud[i].name, stud[i].tel);
else
printf("Can not find..\n");
}

回答3:

#include "stdio.h"
#include "string.h"
struct aa
{
char name[10];
char tel[20];
};

void main()
{
struct aa stud[]={"Liming","88776655","Wangping","88665544","zhanghua","88554433"};
char nn[10];
int i;
gets(nn);

for(i=0; i<3; i++)
if(strcmp(stud[i].name, nn)==0)
break;
if(i < 3)
printf("name:%s tel:%s\n", stud[i].name, stud[i].tel);
else
printf("Can not find..\n");
}