【发布时间】:2021-09-07 05:00:35
【问题描述】:
当程序运行时,在它的中间(在输入疫苗名称后)它会停止并给我 {Exception Thrown} 谁能帮我解决这个问题?我还附上了它给我的错误的照片
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int choose, dosage, quantity;
char code[10];
char name[32];
char produce[64];
float population;
{
printf("COVID-19 Vaccination System \n");
printf("1:- Inventory Creation \n");
printf("2:- Update Vaccine Quantities \n");
printf("3:- Search Vaccine \n");
printf("4:- Produce a list of all Vaccines and their distributed Quantities \n");
printf("5:- EXIT \n\n");
printf("==>");
scanf_s("\n%d", &choose);
if (choose == 1)
{
char ch;
FILE* cv;
errno_t err;
err = fopen_s(&cv, "vaccine.txt", "w");
for (int i = 0; i < 5; i++)
{
printf("Please Enter Vaccine Full information %d \n", i + 1);
printf(" Name of Vaccine : ");
scanf_s("\n%s", &name);
printf("Please Enter Vaccine Code %c \n", i + 1);
printf(" Vaccine Code : ");
scanf_s("\n%s", &code);
printf("Please Enter Vaccine Producing Country %c \n", i + 1);
printf("Producing Country : ");
scanf_s("\n%s", &produce);
printf("Please Enter Dosage Required %d \n", i + 1);
printf("Dosage Required (MAX=2 - MIN=1) : ");
scanf_s("\n%d", &dosage);
printf("Please Enter Population Covered %d \n", i + 1);
printf("Population Covered (%) : ");
scanf_s("\n%f", &population);
printf("Please Enter Vaccine Quantity %d \n", i + 1);
scanf_s("\n%d", &quantity);
fprintf("%s %s %s %d %f %d", name, code, produce, dosage, population, quantity);
}
}
}
return 0;
}
【问题讨论】:
-
docs.microsoft.com/en-us/cpp/c-runtime-library/reference/… [... 指定所有 c、C、s、S 或字符串控制集 [] 参数的大小。 ...]
-
根据this,无论何时使用
%s,scanf_s都应该有两个参数——一个参数是指向缓冲区的指针,第二个参数是缓冲区的大小。跨度> -
是否存在不需要 size 参数的 scanf_s() 重载?如果没有,它是如何构建的?
-
你为什么用
scanf_s而不是scanf?是因为 Microsoft Visual Studio 告诉你这样做的吗?如果是这种情况,那么我建议您禁用此错误消息,方法是在代码开头添加#define _CRT_SECURE_NO_WARNINGS(在任何#include指令之前)。 -
@AndreasWenzel 这是个坏建议。
scanf可以溢出输入缓冲区。scanf_s的存在是有原因的(很遗憾它没有被其他供应商采用)。