#Scanf error

34 messages · Page 1 of 1 (latest)

wind karma
limpid galleonBOT
#

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.

limpid galleonBOT
#
abcq2
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.

wind karma
#

!solved

limpid galleonBOT
#

Thank you and let us know if you have any more questions!

wind karma
#

@stiff lindenwait nvm, what is that supposed to solve

stiff linden
#

i literally can't see what you posted

#

because you posted a screenshot

wind karma
#

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

stiff linden
#

goto emil

wind karma
#

my 2 weeks experience in C trolol

jolly mist
wind karma
jolly mist
#

depends how you are using the goto

wind karma
#

going to a menu

#

(back to a menu)

jolly mist
#

you would need to restructure your code, however you do that is up to you

stiff linden
wind karma
stiff linden
#

scanf takes pointers

#

so scanf("%d",inserir[i].idade); probably isn't right

stiff linden
#

i don't know what inserir is but probably you meant scanf("%d", &inserir[i].idade);

wind karma
#

so i do have to use a & with structs

stiff linden
#

no

#

you have to use & when you want a pointer

wind karma
#
                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);```