Jump to content

Float Value Verification

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
howejustin

howejustin

    Newbie

  • Members
  • Pip
  • 8 posts
Hi all,

I'm new to programming in C and am trying to grasp the concept of verifying input from a float value. My question is, with the following example, how can I verify that the user has entered a positive float value (ex: 324.98) and not a negative number or alphabetic number? Thanks!

#include <stdio.h>

#include <stdlib.h>


main()

{

   

    float fltInput;

    int ui;

  

    printf("\nPlease enter a dollar amount:  ");

    ui = scanf("%f",&fltInput); 

    fflush(stdin);  

    

     if (ui == 0) 

    {

        printf("invalid!\n");

    }

    else

    {

        printf("valid!  you entered %f\n", fltInput);

    }

        


    printf("press <enter> to continue\n");

    getch();

}



#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Instead of
if (ui == 0)
you want
if (ui <= 0)

Numeric input is likely to cause an error that results in a crash.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
howejustin

howejustin

    Newbie

  • Members
  • Pip
  • 8 posts
Thanks WingedPanther!

The only thing I noticed with that is, a user can still enter a negative numeric value and the program will continue to function using that value. Also, since it's a float input, a user can enter a number with a letter at the end and it will just ignore the letter. Is there any way to not allow -any- letters or negative numbers, causing the invalid message to appear?

Also, how could I loop the program back to the prompt to enter a value once the invalid message comes up, instead of it jumping to the end of the program?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
If you make the change I recommended, you will get the "invalid" error on negatives. If you want to prevent users from entering numerics, you will probably have to create your own input handling routine where you inspect each character as it is typed, and return some sort of code indicating the type of data entered while storing the entered data in a parameter that was passed by reference.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
howejustin

howejustin

    Newbie

  • Members
  • Pip
  • 8 posts
Thanks for the prompt reply. I must have something else in there affecting it then, because when I enter any negative number, the program still functions as if nothing is wrong. Here is my program (again, I'm new at C, so I'm sure some of the coding isn't optimal).
#include <stdio.h>

#include <stdlib.h>


main()


{


//Define the Variables and their Values


float fltDelMar = .0725f;	//Defines Del Mar's tax percentage

float fltEncinitas = .075f;	//Defines Encinitas' tax percentage

float fltLaJolla = .0775f;	//Defines La Jolla's tax percentage

float fltAmount;		//Defines the sale amount

char chrPercent = '\45';	//Defines a % sign for usage in the program

char chrEqual = '\75';		//Defines an = sign for usage in the program

char chrColon = '\72';		//Defines a : sign for usage in the program

int ui;				//Defines an integer to hold the scanf function for user input to be verfied


//Displays what the program is and what it will do

printf("Kudler Fine Foods Tax Calculator\n");

printf("\nThis program will calculate the tax amount that should be added to each sale\n based on the following locations and their appropriate tax rates. \nIt will also provide the total amount for the transaction.\n");

printf("\nDel Mar - 7.25%c tax rate", chrPercent);

printf("\nEncinitas - 7.5%c tax rate", chrPercent);

printf("\nLa Jolla - 7.75%c tax rate\n", chrPercent);


//Allows the user to enter an amount to be taxed

printf("\nPlease enter the total purchase amount in dollars and cents, then press [ENTER]:\n");

    ui = scanf("%f",&fltAmount); 

    fflush(stdin);  

    

     if (ui <= 0) 

    {

        printf("That is not a valid purchase amount.\n");

    }

    else

    {


//Calculates the tax rates as well as totals the transaction

printf("\nDel Mar%c Tax%c $%.2f, Total%c $%.2f\n", chrColon, chrEqual, fltDelMar * fltAmount, chrEqual, (fltDelMar * fltAmount) + fltAmount);

printf("\nEncinitas%c Tax%c $%.2f, Total%c $%.2f\n", chrColon, chrEqual, fltEncinitas * fltAmount, chrEqual, (fltEncinitas * fltAmount) + fltAmount);

printf("\nLa Jolla%c Tax%c $%.2f, Total%c $%.2f\n", chrColon, chrEqual, fltLaJolla * fltAmount, chrEqual, (fltLaJolla * fltAmount) + fltAmount);

    }

//Requires the user to press [ENTER] to exit the program

printf("\nThank you for using the tax calculator!");

printf("\nPress [ENTER] to exit the program...");

getch();


}


#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Have you actually outputted the value you are getting in? Using the wrong conversion specifier on your scanf, for example, can cause interesting issues.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
howejustin

howejustin

    Newbie

  • Members
  • Pip
  • 8 posts
I have just what the program above states...so I'm all ears on what I need to do. Thanks again for your assistance.

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Why is ui declared as an int?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
howejustin

howejustin

    Newbie

  • Members
  • Pip
  • 8 posts
Good question, no real reason besides that I'm too new to C to know any better. I figured it was the best out of char, float and int. If there's a better way to do it, please divulge. This is only my second C program.

#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
If you are going to use it as a float, you have to declare it a float. Declaring it int will result in a completely different encoding occurring, so that a negative float might be converted to a positive int. If you want decimal values, you need a float.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog