Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My second scanf is not working - what can I do?

Tags:

c

scanf

clion

My second scanf in this code is not working, the computer doesn't wait for me to enter the second value.

I know when it happens with %c it is just need to put a space before the % But can't make it work with %f

#include <stdlib.h>
#include <stdio.h>

main()
{
    float a, p;
    printf("Insira seu peso: "); scanf("%.1f", &p);
    printf("Insira sua altura: "); scanf("%.2f", &a);
}
like image 728
Tails Avatar asked Oct 22 '25 05:10

Tails


2 Answers

The dot . is not a valid format flag character in the scanf() family of functions. The formats are related to the printf() family of formats (and that's not accidental) but they are far from identical.

Each conversion specification is introduced by the character '%' … after which the following appear in sequence:

  • An optional assignment-suppressing character '*'.

  • An optional non-zero decimal integer that specifies the maximum field width.

  • [CX] ⌦ An optional assignment-allocation character 'm'. ⌫

  • An option length modifier that specifies the size of the receiving object.

  • A conversion specifier character that specifies the type of conversion to be applied. The valid conversion specifiers are described below.

No mention of . in there.

like image 64
Jonathan Leffler Avatar answered Oct 23 '25 21:10

Jonathan Leffler


Actually not a single scanf is working properly in the code. When we use scanf we are just going to get the answer from the user so we do not need a (.) before the variable "f", float in there. We can make adjustments when we print the number that the user has put. Below I have shown an example:

#include<stdio.h>
#include<stdlib.h>


int main()
{
float a, p;
printf("\n Please enter 1st value: ");
scanf("%f", &p);
printf("\n Please enter 2nd value: ");
scanf("%f", &a);

printf("\n The value of p is: %.1f", p);
printf("\n The value of a is: %.2f", a);



printf("\n\n");

system("PAUSE");
return(0);
} 
like image 33
Bixo Avatar answered Oct 23 '25 22:10

Bixo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!