Error comparing a code in C

Asked

Viewed 44 times

0

I am starting in C learning and am having trouble with the following code: `

float piValue;
float userAnswerPI;
char userAnswerYN;

piValue = 3.1416;

printf("What's the value of PI?");
scanf("%f", &userAnswerPI);

// Analisando a resposta do valor de PI
if(userAnswerPI !=3.14||userAnswerPI !=3.1416) {
    printf("Are you sure?");
    scanf(" %c", userAnswerYN);
} 

system("pause");
return 0;

` When I add the values 3.14 in the scanf or the value 3.1416, if is triggered and it was not meant to happen, please help me :/

  • It is not possible to compare floats, because when inserting 3.14 the variable will contain something like 3.140000012312, there are many topics about comparison with floats.

  • One way to solve this was to read the value as string and compare the value with another string, thus solving the problem

  • Also: https://answall.com/q/126967/101 and https://answall.com/q/244836/101

No answers

Browser other questions tagged

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