I’ve this problem, my nome’s is a char[20], if I insert some character the array comes into overflow and write in the other array (cognome).
I’ve tried to change the scanf() with puts(), but this last one wont work properly because it jump off the input and doesn’t read nothing.
Someone know solutions?
Here is the code
void addStudent(){
struct student s;
printf("Inserire Nome: ");
scanf("%20s",s.nome);
printf("\nInserire Cognome: ");
scanf("%30s",s.cognome);
printf("\nInserire eta': ");
scanf("%d",&s.anni);
}
Result
Inserire Nome: cjhsdjkhbsdkhfgsdjkhfgskjhgjkhsfs
Inserire Cognome:
Inserire eta':
Change to
"%19s". It must be one less than the array size asscanf()writes a null terminator. After the calls toscanf()you need to skip any unprocessed input, which can be achieved by reading until the next new-line character: