I'm comparing a string taken in input by a normal scanf with a string in a struct. This is the struct:
typedef struct{ // Tipo giocatore e i suoi dati
int id;
char * nome;
char * cognome; //<--------- this one (it means surname)
int eta;
char * ruolo;
squadra team;
char * college;
int td;
} giocatore;
And this is the code that work and doesn't work (?.?). I don't know why some times it works and some times it doesn't because this is the second case of a switch which has the same identical code as the first case but with the word "name" instead of "surname". Remeber that the first case work every time, the second one on is choose (lol) and the last one (3rd) contain the first problem that i explained before.
size_t ricerca(short int tipo, size_t sz, giocatore array[]){
size_t count = 0;
char cerca;
switch(tipo){
case 1:
printf ("Inserisci il nome da cercare: ");
scanf ("%s", &cerca);
for (size_t i = 0; i < sz; ++i){
if (strcmp(array[i].nome, &cerca) == 0){
printf("ID:%d Nome:'%s' Cognome:'%s' Eta:%d Ruolo:'%s' Team:'%s' College:'%s'\n",
array[i].id,
array[i].nome,
array[i].cognome,
array[i].eta,
array[i].ruolo,
array[i].team.nome,
array[i].college);
count++;
}
}
break;
case 2:
printf ("Inserisci il cognome da cercare: ");
scanf ("%s", &cerca);
for (size_t i = 0; i < sz; ++i){
if (strcmp(array[i].cognome, &cerca) == 0){
printf("ID:%d Nome:'%s' Cognome:'%s' Eta:%d Ruolo:'%s' Team:'%s' College:'%s'\n",
array[i].id,
array[i].nome,
array[i].cognome,
array[i].eta,
array[i].ruolo,
array[i].team.nome,
array[i].college);
count++;
}
}
break;
case 3:
goto Cleanup;
Cleanup: ;
char nomesq[30];
printf ("Inserisci il nome della squadra da cercare: ");
scanf("%[^\n]%*c", nomesq);
for (size_t i = 0; i < sz; ++i){
if (strcmp(array[i].team.nome, nomesq) == 0){
printf("ID:%d Nome:'%s' Cognome:'%s' Eta:%d Ruolo:'%s' Team:'%s' College:'%s'\n",
array[i].id,
array[i].nome,
array[i].cognome,
array[i].eta,
array[i].ruolo,
array[i].team.nome,
array[i].college);
count += 1;
}
}
break;
default:
printf("Inserisci un valore valido\n;");
break;
}
return count;
}
Thanks in advance for your patience and your help and sorry for my english :)