标题:查找数组内元素进行比较输出时的问题
只看楼主
氻乐
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2019-11-9
结帖率:0
 问题点数:0 回复次数:4 
查找数组内元素进行比较输出时的问题
    char name[20];
                printf("请输入您要查询的姓名\n");
                scanf("%s",&name);
                for(i=0;i<400;i++)
                {
                    
                    if(strcmp(students[i].name,name)==0)//对比查找
                    {
                        printf("学生信息为:\n");
                        printf("学号:    %s\n",students[i].number);
                        printf("姓名:     %s\n",students[i].name);
                        printf("性别:    %s\n",students[i].gender);
                        printf("年龄:    %d\n",students[i].age);
                        printf("专业班级:%s\n",students[i].major);
                     }
                     else if(i==399 && strcmp(students[i].name,name)==0)
                     {
                          printf("查无此人");
                          break;
                     }
                     else
                     {
                         continue;
                     }        
此段程序可以顺利进入 比较环节但是如果换成这段
                char number[20];
                printf("请输入您要查询的学号\n");
                scanf("%s",&number);
                for(i=0;i<400;i++)
                {
                    if(strcmp(students[i].number,number)==0)
                    {
                        printf("学生信息为:\n");
                        printf("学号:    %s\n",students[i].number);
                        printf("姓名:     %s\n",students[i].name);
                        printf("性别:    %s\n",students[i].gender);
                        printf("年龄:    %d\n",students[i].age);
                        printf("专业班级:%s\n",students[i].major);
                    }
                    else if(i==399 && strcmp(students[i].number,number)==0)
                     {
                          printf("查无此人");
                          break;
                     }
                     else
                     {
                         continue;
                     }                    
便无法进行比较,请问是哪里出了问题,我又应当如何修改
谢谢
搜索更多相关主题的帖子: number 比较 strcmp printf name 
2019-11-22 23:39
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:432
帖 子:10064
专家分:41463
注 册:2014-5-20
得分:0 
"无法进行比较"是什么意思?


[此贴子已经被作者于2019-11-23 05:37编辑过]

2019-11-23 05:35
氻乐
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2019-11-9
得分:0 
回复 2楼 吹水佬

就像这个 姓名就可以查询但是学号却不可以
2019-11-23 08:27
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:432
帖 子:10064
专家分:41463
注 册:2014-5-20
得分:0 
之前读取文件数据时没有考虑换行符,上一行的换行符被加到下一行number头部。
fscanf(fin,"%[^,],%[^,],%[^,],%d,%s",students[i].number, students[i].name, students[i].gender, &students[i].age, students[i].major)

改为:
fscanf(fin,"%[^,],%[^,],%[^,],%d,%s\n",students[i].number, students[i].name, students[i].gender, &students[i].age, students[i].major)



[此贴子已经被作者于2019-11-23 12:10编辑过]

2019-11-23 12:09
氻乐
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2019-11-9
得分:0 
回复 4楼 吹水佬
谢谢,学会了,
2019-11-23 13:42



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-498003-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.175513 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved