I am attempting to use a single error checking function to check for errors within multiple other functions. However, When running the program, it gets stuck in a loop and won't break from it. Once the application gets a valid input, it should continue to the next function. However it repeats the fStartBalance function even if the error checking returns a 0. I'm kind of a beginner, so I'm not too good at troubleshooting. Here is the applicable code:
/* Begin fValidateFloat */
int fValidateFloat(float input, float minValue, float maxValue)
{
int failed = 0;
while (input < minValue)
{
printf("\nError: Input too low, please enter a value greater than or equal to %.2f.", minValue);
failed = 1;
return failed;
}
while (input > maxValue)
{
printf("\nError: Input too high, please enter a value less than or equal to %.2f.", maxValue);
failed = 1;
return failed;
}
} /* End fValidateFloat */
/* Begin fStartBalance */
float fStartBalance(void)
{
float startBalance; /* Declare variable */
int failed;
while (failed = 1)
{
printf("\n\nNow enter your current balance in dollars and cents: "); /* Prompt user to input current balance */
scanf("%f", &startBalance);
fflush(stdin);
failed = fValidateFloat(startBalance, 0, 3.4e+38);
}
return startBalance; /* Return variable */
} /* End fStartBalance */
Thanks for any help.
fflush(stdin);
is undefined
You never return any value from fValidateFloat()
in the case the while statements are not taken:
return failed;
}
} /* End fValidateFloat */
In the same function while loops don't make any sense, use an if statement instead:
while (input < minValue)
{
printf("\nError: Input too low, please enter a value grea%.2f.", minValue);
failed = 1;
return failed;
}
You are setting failed to 1 in the while loop, instead of comparing it to 1:
while (failed = 1)