【问题标题】:Program stops when inputting data for the scanf_s为 scanf_s 输入数据时程序停止
【发布时间】:2015-04-12 14:38:31
【问题描述】:

程序:

#include "stdafx.h"
#include "stdio.h"
#include "string.h"
//#include "iostream"
const int IDLEN = 10;
const int POLARITYLEN = 3;
const int MAXSTOCKITEMS = 10;
struct TransistorRec {
    char manufacturersID[IDLEN + 1];
    char polarity[POLARITYLEN + 1];
    float power;
    float gain;
    int stock;
};
typedef struct TransistorRec Transistor;
struct StockRec{
    int size;
    Transistor stocklist[MAXSTOCKITEMS];
};
typedef struct StockRec Stock;
int main()
{
    int total, i;
    struct TransistorRec a[10];
    char x, y;
    printf("How many transistors: ");
    scanf_s("%i", &total);
    if (total >MAXSTOCKITEMS){
        printf("too much!! repeat");
        scanf_s("%i\n", &total);
    }
    for (i = 0; i < total; i++)
    {
        printf("Enter manufacturer's ID of transistor:");
        scanf_s("%s",a[i].manufacturersID);
        printf("Enter polarity of transistor: ");
        scanf_s("%s",a[i].polarity);
        printf("Enter power of transistor: ");
        scanf_s("%f",a[i].power);
        printf("Enter gain of transistor: ");
        scanf_s("%f",a[i].gain);
        printf("Enter current stock of transistor: ");
        scanf_s("%i",a[i].stock);
    }

    return 0;
}

我的任务:

编写一个 C 函数,从用户(键盘)读取有关库存晶体管的信息, 并将此信息存储在 Stock 结构中。一组输入数据样本(制造商 ID、 极性、最大功率、电流增益、库存数量)为: 2N2222 NPN 0.5 75 23 BC559 PNP 0.5 125 7 TIP31B NPN 40.0 20 11

问题:
当我通过scanf_s 将我的值(字符串)输入到结构时,程序将停止。

【问题讨论】:

  • 走开,自己写吧。
  • 我的 x 曾经是 [i].manufacturersID
  • 和y是a[i].polarity,我只是忘了改回来,
  • 然后使用scanf_s("%s",a[i] .manufacturersID,sizeof(a[i].manufacturersID));scanf_s("%s",a[i].polarity,sizeof(a[i].polarity));
  • 从第二个scanf_s 中删除\n,而不是第一个。第一个没有它...我的错误

标签: c string struct


【解决方案1】:

引用the documentation of scanf_s

备注:

[...]

scanfwscanf 不同,scanf_swscanf_s 需要为cCsS 或字符串类型的所有输入参数指定缓冲区大小包含在[] 中的控制集。以字符为单位的缓冲区大小作为附加参数传递,紧跟在指向缓冲区或变量的指针之后。

所以,scanf_ss:

scanf_s("%s",a[i].manufacturersID);
scanf_s("%s",a[i].polarity);

需要第三个参数来指定上面引用中提到的缓冲区大小。所以,使用

scanf_s("%s",a[i].manufacturersID,sizeof(a[i].manufacturersID));
scanf_s("%s",a[i].polarity,sizeof(a[i].polarity));

scanf_s("%s",a[i].manufacturersID,_countof(a[i].manufacturersID));
scanf_s("%s",a[i].polarity,_countof(a[i].polarity));

还有其他错误。这些:

scanf_s("%f",a[i].power);
scanf_s("%f",a[i].gain);
scanf_s("%i",a[i].stock);

需要

scanf_s("%f",&a[i].power);
scanf_s("%f",&a[i].gain);
scanf_s("%i",&a[i].stock);

因为 %f%i 分别需要 float*int*,而不是 floatint

至于为什么前两个scanf_s 不需要&amp;,因为在C 中,数组名称“衰减”为指向其第一个元素的指针。 a[i].manufacturersIDa[i].polarity 是数组,“衰减”会自动发生。

您还应该从

中删除\n
scanf_s("%i\n", &total);

因为\n 被视为空白字符,并且此字符将丢弃任意数量的空白字符,包括无直到第一个非空白字符,如 C11 标准中所指定:

7.21.6.2 fscanf 函数

[...]

  1. 由空白字符组成的指令通过读取输入直到第一个非空白字符(仍然未读取)或直到无法读取更多字符来执行。该指令永远不会失败。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 2016-12-21
    相关资源
    最近更新 更多