1

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.

5
  • 1
    Look at the while loop: you want to check if failed is equal to 1. Instead, though, you are setting it to 1! Use failed == 1 instead. Commented Dec 2, 2014 at 2:00
  • Remember to turn all your warnings on. At the very least use -Wall if you are using gcc. It will warn you about silly mistakes like your failed = 1 typo. Commented Dec 2, 2014 at 2:16
  • the 'while' statements in function: fValidateFloat never execute in a loop, so would be better written as 'if' statements Commented Dec 2, 2014 at 4:05
  • regarding that (previously commented) while loop by @Riley, the variable 'failed' is not initialized to any specific value (just what ever happens to be on the stack at that address. Suggest: set the 'failed' variable to some known value when declaring it. this is also a prime example of why the literal should be on the left side, so the compiler would catch these kinds of errors, so you don't have to. Commented Dec 2, 2014 at 4:07
  • the call to scanf needs attention for: 1) the format string needs a leading ' ' to consume white space, like the prior newline 2) the returned value needs to be checked to assure the operation/format conversion(s) was successful Commented Dec 2, 2014 at 4:10

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.