This code was originally for an assignment. I met the requirements of the assignment but wish to expand the code to further check for valid input. I am quite new at this but trying to learn...

Anyway, the problem is this. I can check if the float is non-negative with a simple loop as such:

int main(void)
{
    //initialize variable
    float fDollars=0.00;
    
    //prompt for and accept input
    printf("Please enter a dollar amount: ");
    scanf("%f", &fDollars);
    
    //check user response as valid
    while (fDollars <= 0.00)
    {
                printf("\nYou entered an invalid response.");
                printf("\nPlease enter a postive amount: ");
                scanf("%f", &fDollars);
    } //end while

    return 0;
} //end main

I can alternatively check to see if the input is not a character with the following:

//Loop until user inputs a valid dollar amount
      while (scanf("%f", &fDollars) != 1)
      {
         while (getchar() != '\n');
         printf ("Please enter a dollar amount: ");
      } //End loop

      printf ("You entered %.2f\n", fDollars);

However, I am not able to check both with any success.

I have read that scanf is not preferred but I am not sure what else to use.

Also, would it be better to accept the float as a string and then convert to a float? I think it would be for validation but do not even know where to begin on that one.

If anyone is interested in looking at the entire program, it is attached. I know I still have some other things to work on with it to make it more robust and user-proof. However, I am using this to learn.

Thanks.

Recommended Answers

All 4 Replies

Try combining the tests like this:

int code;

while ((code = scanf("%f", &fDollars)) != 1 || fDollars <= 0)
{
    printf("invalid amount. please enter a positive amount: ");
    fflush(stdout);

    if (code != 1) FLUSH_STDIN();
}

Further reading for extra credit can be found here.

Try combining the tests like this:

int code;

while ((code = scanf("%f", &fDollars)) != 1 || fDollars <= 0)
{
    printf("invalid amount. please enter a positive amount: ");
    fflush(stdout);

    if (code != 1) FLUSH_STDIN();
}

Further reading for extra credit can be found here.

Thanks, I had been thinking along those lines previously--trying to combine them. However, when combined, the program will accept an input and go to a new line on carriage return but will not continue the loop until a character is entered.

Your code is similar to what I tried and I get the same result. How could I input as a string and then compare and convert to a double? I think that atof would serve that purpose but I am not sure of the proper syntax.

scanf() does not treat new line characters as different from other white space. To get line oriented input you should use something like fgets(). sscanf() is an easy way to parse the line fgets() reads:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char line[BUFSIZ];
    float dollars;

    while (fgets(line, sizeof line, stdin) &&
           sscanf(line, "%f", &dollars) != 1 ||
           dollars <= 0)
    {
        printf("invalid amount. please enter a positive amount: ");
        fflush(stdout);
    }

    printf("Received %f\n", dollars);

    return 0;
}

Works like a charm! I will have to dissect all of that and figure out why it works like this. Thanks for helping me figure this out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.