| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1268 人关注过本帖
标题:求指教貌似循环中的 scanf 无效
只看楼主 加入收藏
top398
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:427
专家分:857
注 册:2014-5-2
收藏
得分:0 
以下是引用kwxx在2014-5-9 06:35:14的发言:

scanf("%d",&n);
后面加一个getchar();消化输入产生的回车。
你实际测试过么?”后面加一个getchar();消化输入产生的回车“,这完全是多此一举。另外他的格式 "%d\t" 并非不可,只是输入有些麻烦。
2014-05-09 08:59
q987456963q
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2013-11-1
收藏
得分:0 
回复 8 楼 kwxx
修改了一下#include<stdio.h>
#include<string.h>
struct student
{
    char Num[10];
    char Name[10];
    int type;
    int total;
};
int main()
{
    struct student stud1[100];
    int i,n;
    int x;
    char k[10];
    printf("input how many students you need to input:");
    scanf("%d",&n);
    printf("input number\tname\tstudenttype(major 1. in science 2.in arts)\ttotal:\n");
    for(i=0;i<n;i++)
    {
        scanf("%s\t",&stud1[i].Num);
        scanf("%s\t",&stud1[i].Name);
        scanf("%d\t",&stud1[i].type);
        scanf("%d\t",&stud1[i].total);
    }                                     /*printf("%s\t",stud1[0].Num);
                                            printf("%s\t",stud1[0].Name);
                                            printf("type %d\t\t",stud1[0].type);
                                            printf("%d",stud1[0].total);
                                            printf("\n");
                                            printf("%s\t",stud1[1].Num);
                                            printf("%s\t",stud1[1].Name);
                                            printf("type %d\t\t",stud1[1].type);
                                            printf("%d",stud1[1].total);
                                            printf("\n");*/
     
     while(1)
    {
       printf("input the number you search:");
       scanf("%s",&k);
       printf("\n");
       for(i=0;i<n;i++)
       {
          if(strcmp(stud1[i].Num,k)==0)
          {
               printf("%s\t",stud1[i].Num);
             printf("%s\t",stud1[i].Name);
             printf("type %d\t\t",stud1[i].type);
             printf("%d",stud1[i].total);
             printf("\n");
          }
       }
       printf("input 1 to input again or 2 to out:");
       scanf("%d",&x);
       if(x==2)
         break;
       printf("\n");
    }
    printf("\n");
    return 0;
}
现在有的问题只是     printf("input the number you search:");
       scanf("%s",&k);
这两句执行时不知道为什么反了过来
至于你说的scanf("%s\t",&stud1[i].Num);
        scanf("%s\t",&stud1[i].Name);
换区符问题,我才刚学到结构体,这两句书上就有同样的句子,是关于字符数组的输入。
2014-05-09 11:18
top398
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:427
专家分:857
注 册:2014-5-2
收藏
得分:0 
楼主依然故我,奈何,奈何!
2014-05-09 11:26
loveClangage
Rank: 8Rank: 8
来 自:广东云浮
等 级:蝙蝠侠
帖 子:326
专家分:891
注 册:2013-8-23
收藏
得分:2 
路过,

编写的程序,不能改变世界,却可以改变自己...
2014-05-09 19:21
kwxx
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:309
专家分:913
注 册:2009-5-11
收藏
得分:0 
11楼说的对,只要你输入格式和你自己制定的格式一致,程序就可以正常输入、输出。出现问题,是你输入的格式有问题。
2014-05-09 20:13
ppvae
Rank: 1
等 级:新手上路
帖 子:25
专家分:2
注 册:2014-1-18
收藏
得分:2 
你那不是循环,就只能查一次而已;应该是while(x==1)
2014-05-09 23:24
ppvae
Rank: 1
等 级:新手上路
帖 子:25
专家分:2
注 册:2014-1-18
收藏
得分:0 
#include<stdio.h>
#include<string.h>
struct student
{
    char Num[10];
    char Name[15];
    int type;
    int total;
};
int main()
{
    struct student stud1[100];
    int i,n;
    int x=1;
    char k[10];
    printf("input how many students you need to input:");
    scanf("%d",&n);

    printf("input number\tname\tstudenttype(major 1. in science 2.in arts)\ttotal:\n");
    for(i=0;i<n;i++)
    {
        scanf("%s",stud1[i].Num);

        scanf("%s",stud1[i].Name);

        scanf("%d",&stud1[i].type);

        scanf("%s",&stud1[i].total);


    }

    printf("\n");
    while(x==1)
    {
       printf("input the number you search:");
       scanf("%s",k);
       printf("\n");
       for(i=0;i<n;i++)
       {
          if(strcmp(stud1[i].Num,k)==0)
          {
             printf("%s\t",stud1[i].Num);
             printf("%s\t",stud1[i].Name);
             printf("type %d\t\t",stud1[i].type);
             printf("%d",stud1[i].total);
             printf("\n");
          }
       }
       printf("input 1 to input again or 2 to out:");
       scanf("%d",&x);
    }

    return 0;
}
运行无误。
2014-05-10 00:02
快速回复:求指教貌似循环中的 scanf 无效
数据加载中...
 
   



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

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