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.
whileloop: you want to check iffailedis equal to 1. Instead, though, you are setting it to 1! Usefailed == 1instead.-Wallif you are usinggcc. It will warn you about silly mistakes like yourfailed = 1typo.