【问题标题】:Trouble reading strings and int in a row with scanf使用 scanf 连续读取字符串和 int 时遇到问题
【发布时间】:2014-10-29 00:48:19
【问题描述】:

我必须读取由空格分隔的 3 个字符串和由空格分隔的 2 个整数,并将它们保存到一个结构中

  #define n 5;
    struct person
    {
        char last_name[20];
        char sec_last_name[20];
        char name[20];
        int num;
        int kw;
    }client[n];

我试着这样读

for(i=0;i<n;i++)
{    
    scanf("%s %s %s %d %d",client[i].last_name,client[i].sec_last_name,client[i].name,client[i].num,client[i].kw);
}

因为我必须连续读取它,但是当我运行程序时,它会在输入第一行数据并按 Enter 后停止,在玩弄我的代码之后,问题与读取整数有关,但我似乎无法解决它。

【问题讨论】:

  • 更改为&amp;client[i].num, &amp;client[i].kw

标签: c string struct int scanf


【解决方案1】:

scanf 语句有语法错误。在您的情况下,&amp; 应在整数之前使用。

 scanf("%s %s %s %d %d",client[i].last_name,client[i].sec_last_name,client[i].name,&client[i].num,&client[i].kw);

还有一点,

#define n 5 /*;*/

; 不应使用。

示例:

如果使用;,则替换client[5;];在结构变量声明中。然后它会发出编译错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多