I am coding in Visual Studio 2017 and I am making a vaccine inventory system. When adding the values to a txxt file however I am getting the error Debug Assertion Failed, I looked online and nothing worked. My code is below
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _CRT_SECURE_NO_WARNINGS
void main() {
printf("Enter 1 for inventory creation \nEnter 2 for Updating Inventor \nEnter 3 for Vaccine Search \nEnter 4 for Distribution Details \n");
int num1;
scanf_s("%d", &num1);
while (num1 != 1 && num1 != 2 && num1 != 3 && num1 != 4) {
printf("Please enter a number between 1 and 4 \nEnter 1 for Inventory Creation \nEnter 2 for Updating Inventor \nEnter 3 for Vaccine Search \nEnter 4 for Distribution Details \n");
num1 = 0;
scanf_s("%d", &num1);
}
if (num1 == 1) {
char vaccineName[60];
char vCode[2];
char vCountry[60];
int vDose;
int popCovered;
int vQuantity;
printf("Enter Vaccine Name: \n");
scanf_s("%s", vaccineName, _countof(vaccineName));
printf("Enter Vaccine Code: \n");
scanf_s("%s", vCode, _countof(vCode));
printf("Enter Production Country: \n");
scanf_s("%s", vCountry, _countof(vCountry));
printf("Enter Vaccine Dosage: \n");
scanf_s("%d", &vDose);
printf("Enter Population Covered: \n");
scanf_s("%d", &popCovered);
printf("Enter Initial Quantity \n");
scanf_s("%d", &vQuantity);
FILE *fp;
fp = fopen_s(&fp, "vaccine", "a");
fprintf(fp, "%s", "\n \n");
fprintf(fp, "Vaccine Name: %s \t", vaccineName);
fprintf(fp, "Vaccine Code: %s \t", vCode);
fprintf(fp, "Production Country: %s \t", vCountry);
fprintf(fp, "Vaccine Dosage: %d \t", vDose);
fprintf(fp, "Population Covered: %d \t", popCovered);
fprintf(fp, "Quantity: %d \t", vQuantity);
fclose(fp);
}
}
I have tried alternative I could find and I do not understand the error. The file name and location in my explorer is shown below
Location of my vaccine.txt file
Any help will be appreciated, thank you!
void main()through. It's wrong anyway. It should beint main(). Btw, I don't think C++ hasscanf_s- Ted Lyngmofopen_sdoesn't return a file pointer. It returns an error value. The file pointer is set because you pass a pointer to it to the function. Since 0 means no error you're overwriting a potentially valid file pointer with 0 or NULL. - Retired Ninja