Not quite sure what im doing wrong here, program kinda just breaks at the third scanf
https://cdn.discordapp.com/attachments/1053483389660975174/1053483541335379978/image.png
#Scanf error
34 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
Please Do Not Send Screenshots!
They're hard to read and prevent copying and pasting.
Handling Whitespace in scanf
When the user presses space or enter in the console,
this puts ' ' and '\n' whitespace
characters into stdio respectively.
When reading an int* with %d or float* with %f, scanf
skips any leading whitespace.
However, %c for char* extracts whitespace characters.
Example Problem
Say the user enters " 70 f", and we want to read 70 and f into int and char respectively:
int x; char c;
scanf("%d", &x); // OK, x = 70
scanf("%c", &c); // BAD, c = ' '
Instead of reading f, we read a space, because %c does not skip leading whitespace. Solution:
scanf(" %c", &c);
The leading space before %c matches any whitespace of any length.
!solved
Thank you and let us know if you have any more questions!
@stiff lindenwait nvm, what is that supposed to solve
i have 3 printf + scanfs
and for some reason its just breaking
printf("Olympia - Jardim Zoologico\nInserir novo animal\n",i++);
inserir[i].numero=i;
printf("Introduza o nome: ");
fflush(stdin);
scanf("%s",inserir[i].nome);
//gets(inserir[i].nome);
printf("Introduza a especie: ");
fflush(stdin);
scanf("%s",inserir[i].especie);
//gets(inserir[i].especie);
printf("Introduza a idade: ");
fflush(stdin);
scanf("%d",inserir[i].idade);
printf("Introduza a data de entrada (dd/mm/aa) : ");
scanf("%d/%d/%d",inserir[i].dia,inserir[i].mes,inserir[i].ano);
printf("Prima ENTER para sair");
getchar();
goto gestaoanimais;
break;```
i know its not in english but just have a look at the commands
goto 
goto is heavily frowned upon by most people
and what would i do to replace a goto statement
depends how you are using the goto
you would need to restructure your code, however you do that is up to you
make it into a function and use return
ohh yeah that'd work, but how about why my scanf doesnt work?
i have no idea what you mean by "breaking" but
scanf takes pointers
so scanf("%d",inserir[i].idade); probably isn't right
yes its not
i don't know what inserir is but probably you meant scanf("%d", &inserir[i].idade);
so i do have to use a & with structs
its still not working (just exits the console after ^)
scanf("%s",insert[i].name);
printf("Introduce the species: ");
scanf("%s",insert[i].species);
printf("Introduce the age: ");
scanf("%d",&insert[i].age);
printf("Introduce the date (dd/mm/aa) : ");
scanf("%d/%d/%d",&insert[i].day,&insert[i].month,&insert[i].year);```
