I'm trying remove line break from input using strtok()
and pass it to struct property using strcpy()
but when i execute this the Visual Studio Code is returning this message:
My code:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <locale.h>
typedef struct
{
char name[50];
char document[50];
char cep[50];
char phone[50];
char address[50];
char birthdate[50];
char email[50];
char diagnosticDate[50];
int age;
char comorbidities[50];
} Ocurrence;
void insert_new_record()
{
Ocurrence new_record;
char comorbity_option[3];
printf("Choose option number: ");
fgets(&comorbity_option, sizeof(comorbity_option), stdin);
switch (comorbity_option[0])
{
case '1':
strcpy(new_record.comorbidities, "Diabetes");
break;
case '2':
strcpy(new_record.comorbidities, "Obesidade");
break;
case '3':
strcpy(new_record.comorbidities, "Hipertensão");
break;
case '4':
strcpy(new_record.comorbidities, "Tuberculose");
break;
case '5':
strcpy(new_record.comorbidities, "Outros");
break;
default:
strcpy(new_record.comorbidities, "Nenhuma");
break;
}
strcpy(new_record.name, strtok(new_record.name, "\n"));
strcpy(new_record.cep, strtok(new_record.cep, "\n"));
strcpy(new_record.address, strtok(new_record.address, "\n"));
strcpy(new_record.phone, strtok(new_record.phone, "\n"));
strcpy(new_record.birthdate, strtok(new_record.birthdate, "\n"));
strcpy(new_record.diagnosticDate, strtok(new_record.diagnosticDate, "\n"));
strcpy(new_record.document, strtok(new_record.document, "\n"));
strcpy(new_record.email, strtok(new_record.email, "\n"));
}
int main()
{
setlocale(LC_ALL, "Portuguese");
show_login();
show_menu();
return 0;
}
On debug mode is possible verify that error is show after line: strcpy(new_record.name, strtok(new_record.name, "\n"));
I have search for this on StackOverflow, but anything help to solve this. Could someone help me?