标题:求指教貌似循环中的 scanf 无效
取消只看楼主
q987456963q
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2013-11-1
结帖率:0
已结贴  问题点数:10 回复次数:2 
求指教貌似循环中的 scanf 无效
#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\t",&stud1[i].Num);
        scanf("%s\t",&stud1[i].Name);
        scanf("%d\t",&stud1[i].type);
        scanf("%d\t",&stud1[i].total);
    }
    printf("\n");
    if(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);
       printf("\n");
    }
    return 0;
}
搜索更多相关主题的帖子: include science number 
2014-05-08 21:32
q987456963q
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2013-11-1
得分:0 
我想问的是if(x==1)中的scanf("%s",&k);和scanf("%d",&x);为什么执行时直接跳过


2014-05-08 21:45
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



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




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

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