当程序运行时,在它中间(在输入疫苗名称后)它会停止并给我 {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;
}
答案 0 :(得分:0)
线
fprintf("%s %s %s %d %f %d", name, code, produce, dosage, population, quantity);
错了。您必须将文件指针作为 fprintf()
的第一个参数传递。应该是:
fprintf(cv, "%s %s %s %d %f %d", name, code, produce, dosage, population, quantity);
还有
fopen_s
是否成功,如果失败则停止操作。&
中使用的数组名称(code
、name
、produce
)之前的 scanf_s
。表达式中的数组(不包括一些例外)会自动转换为指向第一个元素的指针,因此此处不需要显式 &
。它还会使类型错误 scanf()
期望 char*
为 %s
,但诸如 &code
之类的东西是指向数组的指针(如 char(*)[10]
)。