Printf does not show all variables

Asked

Viewed 124 times

0

I am studying C/C++ in college and came across the following problem, when I run the following code:

    #include <stdio.h>

    void main(){

    char name[10];
    int faltas;
    double media;

    printf("Digite seu nome, quantidade de faltas e sua media: \n");
    scanf("%s" "%d" "%.2f", &name, &faltas, &media);
    printf("Ola %s, voce tem %d faltas e sua media e %.2f",name,faltas,media);
}

Only the result does not show the mean value, as shown in the image below: inserir a descrição da imagem aqui

I wonder what I might be doing wrong, because in the exercise given by the teacher the resolution is the same as the code I put above.

Thanks in advance!!!

  • 1

    The scanf does not know (and has no simple way to solve) that you are using "," to separate decimals on average - use the decimal point "." and should work.

2 answers

1


William,

Your scanf is incorrect, the double reading is done with lf, you also opened and closed the string more than once in the first parameter, the string reading (char array with %s) is not sent with & in front as it is an array.


To fix, open only once the string of the first parameter, at the value %.2f, replace by lf, and when sending the name, do not use &, the result will be the following:

scanf("%s %d %lf", name, &faltas, &media);

Your main function is with the void return, this even compiles, but it must be generating Warning, because normally the main returns an integer, int main(), you can return 0 only as test.

  • With the refinement of cruelty that the & the most in the name in the call for scanf makes the program use the variable that should contain the pointer to the address of the 10 bytes reserved to place the sequence of typed characters. C is like this - a little mistake like this and you just don’t take a segfault by luck, and it still looks like the program works (the name is printed, after all, despite the data being in an arbitrary area of memory )

  • Hello Daniel, program keeps returning the same result, shows name, amount of fouls, but the average remains zeroed

  • William, this shouldn’t happen, you’ve recompiled your program? Check out this online example https://repl.it/repls/GraveEveryOpensource or this one https://repl.it/repls/MotherlyVoluminousConditionals

  • Daniel, I removed the previous executable and compiled it again, went around and worked. Thank you so much for your help!!!!!

  • Show, as for the array pointer, take a look at this response from Maniero, very cool: https://answall.com/questions/321827/por-que-precise

  • Leave it, I’ll take a look!

Show 1 more comment

0

You used the "%f" float formatters instead of the double "lf" formatters in both the scanf and printf. Your string reading is also wrong since it is an array, I would use the gets command to read this specific string, clear the buffer and then read the other data from the program.

  • Good morning, because it is a college exercise, I had not yet seen the function cited by you, but thanks for the tip!!! Ps.: The teacher had passed the resolution of this wrong printf (In the form that lies in my question).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.