gpt4 book ai didi

c - 为 scanf_s 输入数据时程序停止

转载 作者:行者123 更新时间:2023-11-30 21:10:28 26 4
gpt4 key购买 nike

程序:

#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;
}

我的任务:

Write a C function that reads from the user (keyboard) the information about the transistors in stock, and stores this information in the Stock structure. A sample set of input data (manufacturer’s ID, polarity, maximum power, current gain, number in stock) is: 2N2222 NPN 0.5 75 23 BC559 PNP 0.5 125 7 TIP31B NPN 40.0 20 11

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

最佳答案

引用自the documentation of scanf_s ,

Remarks:

[...]

Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or string control sets that are enclosed in []. The buffer size in characters is passed as an additional parameter immediately following the pointer to the buffer or variable.

所以,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 不需要 &,因为在 C 中,数组名称“衰减”到指向其第一个元素的指针。 a[i].manufacturersIDa[i].polity 是数组,“衰减”会自动发生。

您还应该从

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

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

7.21.6.2 The fscanf function

[...]

  1. A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read. The directive never fails.

关于c - 为 scanf_s 输入数据时程序停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29590626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com