comparison of floats

Asked

Viewed 882 times

10

This program allows you to check which of the three floats is the largest, only it is ignoring the decimal places.

For example if I put the values 1.4, 1.6 and 1.5, the program tells me that the greatest is the value 1.4. How do I solve the problem??

void main()
{ 
  setlocale(LC_ALL, "portuguese"); 
  double numero1,numero2,numero3; 
  printf(" introduza o 1º numero, 2º numero e 3º numero!\n"); 
  scanf("%f %f %f", &numero1, &numero2, &numero3); 
  if (numero1 > numero2 && numero1 > numero3) 
  printf(" O 1º número introduzido é o maior!\n");
  else if (numero2 > numero1 && numero2 > numero3) 
  printf(" O 2º número é o mairo!\n"); 
  else printf(" O 3º numero é o maior!\n"); 
}
  • Checks whether the scanf is getting the numbers properly with a printf. Is?

  • The culture you set up (English) uses comma as decimal place. Use a dot culture for decimals, or use , when typing the numbers.

1 answer

10


  double numero1, numero2, numero3;
  scanf("%f %f %f", &numero1, &numero2, &numero3);

Error: the specification "%f" of scanf() waits for a pointer to a variable of the type float, but you’re passing a pointer to a type variable double.

Or do you change the varietal type (do not advise, always use double) or change the specification in scanf() and make sure everything went well

  if (scanf("%lf%lf%lf", &numero1, &numero2, &numero3) != 3) /* erro */;
  • thank you works :)

  • @pmg, your answer worked. I don’t know if mine is correct, but if you think so, incorporate it into yours so I can report mine.

  • @pmg the idea was to get him there alone ;) +1

Browser other questions tagged

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