I wanted to get the positive float using the get_positive_float function, which I declared later. However, it either accepts -0.99999 or rejects 0.999999.
The code looks as follows:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
float get_positive_float(string prompt);
int main(void)
{
float f = get_positive_float("Number: ");
printf("Input: %f\n", f);
}
float get_positive_float(string prompt)
{
int n;
do
{
n = get_float("%s", prompt);
}
while (n <= 0);
return n;
}
The problem is the when n is set to strictly less then 0 after while
, I get the following output after testing:
Number: -0.99999999999999
Number: -0.99999999999
Number: -0.99999999
Number: -0.999999
Input: 0.000000
When =
is allowed, the output is the following:
Number: 0.9
Number: 0.99999999
Input: 1.000000
I cannot get it to reject numbers slightly lower than zero without rejecting numbers slightly above zero. Where do I go wrong?