Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > verify float number

Reply
Thread Tools

verify float number

 
 
Xancatal
Guest
Posts: n/a
 
      10-23-2006
Hey everybody. I need help on this one. I need to verify that a number
entered by a user is not either a negative number (-100.00), or an
alphabet (a, b, c, X, Y) as well as other number other than positive
integers or a decimal point. For example:

Enter amount:

and was capturing the float varialbe as in:

scanf ("%f", &myVar)

I was using scanf to capture the data, but I'm having a hard time
verifying this float with isdigit or isalpha. Any ideas would be
greatly appreciated.

Thanks

 
Reply With Quote
 
 
 
 
Bill Medland
Guest
Posts: n/a
 
      10-23-2006
Xancatal wrote:

> Hey everybody. I need help on this one. I need to verify that a number
> entered by a user is not either a negative number (-100.00), or an
> alphabet (a, b, c, X, Y) as well as other number other than positive
> integers or a decimal point. For example:
>
> Enter amount:
>
> and was capturing the float varialbe as in:
>
> scanf ("%f", &myVar)
>
> I was using scanf to capture the data, but I'm having a hard time
> verifying this float with isdigit or isalpha. Any ideas would be
> greatly appreciated.
>
> Thanks

Sorry; it doesn't work that way. C is a lower level language than that.
If you use scanf then it will do its best to read in and do the conversion
for you (and stop once it gets to a bit that doesn't fit)
If you really want to do low level checking you will need to read in the
string as a string and then parse it yourself.
However you can test to see if the number is negative.
Why don't you just read the number in as an integer?

--
Bill Medland
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      10-23-2006
Xancatal wrote:
>
> Hey everybody. I need help on this one. I need to verify that a number
> entered by a user is not either a negative number (-100.00), or an
> alphabet (a, b, c, X, Y) as well as other number other than positive
> integers or a decimal point. For example:
>
> Enter amount:
>
> and was capturing the float varialbe as in:
>
> scanf ("%f", &myVar)
>
> I was using scanf to capture the data, but I'm having a hard time
> verifying this float with isdigit or isalpha. Any ideas would be
> greatly appreciated.


if ((1 != scanf(%f, &myvar)) || (myvar < 0)) {
/* handle bad entry */
}

Always check input routine calls for errors.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

 
Reply With Quote
 
Xancatal
Guest
Posts: n/a
 
      10-23-2006
Thanks for your response Bill. Do you mean read as integer and then
parse it? How about converting it then to float? would that be
possible? Do you have maybe an example I can go by?

I wast thinking I could use getchar to capture the stream from the
float (or the user), do the verification (you know... No weird
characters like $&# and so on as well as no letters), and then
calculate my float vars. Waddaya think?

Bill Medland wrote:
> Xancatal wrote:
>
> > Hey everybody. I need help on this one. I need to verify that a number
> > entered by a user is not either a negative number (-100.00), or an
> > alphabet (a, b, c, X, Y) as well as other number other than positive
> > integers or a decimal point. For example:
> >
> > Enter amount:
> >
> > and was capturing the float varialbe as in:
> >
> > scanf ("%f", &myVar)
> >
> > I was using scanf to capture the data, but I'm having a hard time
> > verifying this float with isdigit or isalpha. Any ideas would be
> > greatly appreciated.
> >
> > Thanks

> Sorry; it doesn't work that way. C is a lower level language than that.
> If you use scanf then it will do its best to read in and do the conversion
> for you (and stop once it gets to a bit that doesn't fit)
> If you really want to do low level checking you will need to read in the
> string as a string and then parse it yourself.
> However you can test to see if the number is negative.
> Why don't you just read the number in as an integer?
>
> --
> Bill Medland


 
Reply With Quote
 
Xancatal
Guest
Posts: n/a
 
      10-23-2006
Wow chuck, you just went way over my head I assume you mean using
the if conditional, but if you don't mind explaining what precisely
would "1 != to scanf..." really mean on this construct? How would this
verify the input is not alphabetic? I'm sorry for my lack of C language
Chuck, I apologize.

Thanks,

CBFalconer wrote:
> Xancatal wrote:
> >
> > Hey everybody. I need help on this one. I need to verify that a number
> > entered by a user is not either a negative number (-100.00), or an
> > alphabet (a, b, c, X, Y) as well as other number other than positive
> > integers or a decimal point. For example:
> >
> > Enter amount:
> >
> > and was capturing the float varialbe as in:
> >
> > scanf ("%f", &myVar)
> >
> > I was using scanf to capture the data, but I'm having a hard time
> > verifying this float with isdigit or isalpha. Any ideas would be
> > greatly appreciated.

>
> if ((1 != scanf(%f, &myvar)) || (myvar < 0)) {
> /* handle bad entry */
> }
>
> Always check input routine calls for errors.
>
> --
> Chuck F (cbfalconer at maineline dot net)
> Available for consulting/temporary embedded and systems.
> <http://cbfalconer.home.att.net>


 
Reply With Quote
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      10-23-2006

"Xancatal" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Wow chuck, you just went way over my head I assume you mean using
> the if conditional, but if you don't mind explaining what precisely
> would "1 != to scanf..." really mean on this construct? How would this
> verify the input is not alphabetic? I'm sorry for my lack of C language
> Chuck, I apologize.
>
> Thanks,


scanf returns the number of fields that it successfully read.
So if the return value of scanf is one, it means
that is successfully read one field. Since you specified the
field to be a float, it successfully read one float value.

However, this may not be enough for you.
What happens if the user types in " 1.34q" ?
Do you want this to be an error? scanf() will return one
here.

Perhaps you should read the whole line in as a string,
then us strtod to check for errors. Note that strtod()
returns a double, not a float.


>
> CBFalconer wrote:
>> Xancatal wrote:
>> >
>> > Hey everybody. I need help on this one. I need to verify that a number
>> > entered by a user is not either a negative number (-100.00), or an
>> > alphabet (a, b, c, X, Y) as well as other number other than positive
>> > integers or a decimal point. For example:
>> >
>> > Enter amount:
>> >
>> > and was capturing the float varialbe as in:
>> >
>> > scanf ("%f", &myVar)
>> >
>> > I was using scanf to capture the data, but I'm having a hard time
>> > verifying this float with isdigit or isalpha. Any ideas would be
>> > greatly appreciated.

>>
>> if ((1 != scanf(%f, &myvar)) || (myvar < 0)) {
>> /* handle bad entry */
>> }
>>
>> Always check input routine calls for errors.
>>
>> --
>> Chuck F (cbfalconer at maineline dot net)
>> Available for consulting/temporary embedded and systems.
>> <http://cbfalconer.home.att.net>

>

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project


 
Reply With Quote
 
jmcgill
Guest
Posts: n/a
 
      10-23-2006
Xancatal wrote:

>>> scanf ("%f", &myVar)

>> if ((1 != scanf(%f, &myvar)) || (myvar < 0)) {
>> /* handle bad entry */
>> }


Your message was an example of top posting. Whether you think it is
right or wrong, you will receive abuse and/or neglect for doing it.
It took me as much time to fix your post as it did to answer your question.

> Wow chuck, you just went way over my head I assume you mean using
> the if conditional, but if you don't mind explaining what precisely
> would "1 != to scanf..." really mean on this construct? How would this
> verify the input is not alphabetic?


Now I would handle it more like this,

int rc;
float myvar;
if( (EOF == (rc=scanf("%f", &myvar))) || (rc !=1) || (myvar < 0.0) ){
/* handle error */
}

This can be done in one line because of the guaranteed left-to-right
evaluation of the logical or's. You could break it down into several
statements for the same result.

rc = scanf("%f", &myvar);
if(EOF != rc){
if(rc == 1){
if(myvar >= 0.0){
/* the value was acceptable */
}
}
} /* silently discards errors */


void error(char *s){
/* just for example */
fprintf(stderr, "%s\n", s);
exit(-1);
}

/* ... */
rc = scanf("%f", &myvar);
if(EOF == rc){
error("premature end of input");
} else if(rc != 1){
error("expected a float, got something else");
} else if(myvar < 0.0){
error("expected a nonnegative float, got something else");
} else {
/* the value was acceptable */
printf("%f\n", myvar);
}


scanf(...) returns a count of the number of successful assignments. In
this case, you have exactly one conversion specification in your format
string, which means the expected return from scanf(...) is 1. Scanf
could also return zero or EOF, either of which would be an error. You
also apparently only want nonnegative input values; I didn't see that
requirement in the maze of top-posting noise. But because myvar is
float in this example, it should be compared to a float 0.0, not an
integer 0 ;

In reality, I prefer not to use scanf(...) at all. I tend to write my
own input handlers based on fgetc(FILE *), and if I do want scanf(...),
I tend to use sscanf(const char *, const char *, ...) (Such a strategy
puts the programmer in more control; I'm not saying scanf(...) is
dangerous in the way gets() is, but I treat it the same.)

 
Reply With Quote
 
Bill Medland
Guest
Posts: n/a
 
      10-23-2006
Xancatal wrote:

> Thanks for your response Bill. Do you mean read as integer and then
> parse it?

No, you don't parse it. if you've read it then the characters have already
been parsed; it's now an int.
> How about converting it then to float? would that be
> possible? Do you have maybe an example I can go by?
>
> I wast thinking I could use getchar to capture the stream from the
> float (or the user), do the verification (you know... No weird
> characters like $&# and so on as well as no letters), and then
> calculate my float vars. Waddaya think?


My first preference would be simply
if (scanf("%d", &myint) != 1)
and accept that some input will be unused.
If I wanted to be more resilient I'd probably use fgets() and strtol().

>
> Bill Medland wrote:
>> Xancatal wrote:
>>
>> > Hey everybody. I need help on this one. I need to verify that a number
>> > entered by a user is not either a negative number (-100.00), or an
>> > alphabet (a, b, c, X, Y) as well as other number other than positive
>> > integers or a decimal point. For example:
>> >
>> > Enter amount:
>> >
>> > and was capturing the float varialbe as in:
>> >
>> > scanf ("%f", &myVar)
>> >
>> > I was using scanf to capture the data, but I'm having a hard time
>> > verifying this float with isdigit or isalpha. Any ideas would be
>> > greatly appreciated.
>> >
>> > Thanks

>> Sorry; it doesn't work that way. C is a lower level language than that.
>> If you use scanf then it will do its best to read in and do the
>> conversion for you (and stop once it gets to a bit that doesn't fit)
>> If you really want to do low level checking you will need to read in the
>> string as a string and then parse it yourself.
>> However you can test to see if the number is negative.
>> Why don't you just read the number in as an integer?
>>
>> --
>> Bill Medland


--
Bill Medland
 
Reply With Quote
 
Xancatal
Guest
Posts: n/a
 
      10-23-2006

Fred Kleinschmidt wrote:

> "Xancatal" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> > Wow chuck, you just went way over my head I assume you mean using
> > the if conditional, but if you don't mind explaining what precisely
> > would "1 != to scanf..." really mean on this construct? How would this
> > verify the input is not alphabetic? I'm sorry for my lack of C language
> > Chuck, I apologize.
> >
> > Thanks,

>
> scanf returns the number of fields that it successfully read.
> So if the return value of scanf is one, it means
> that is successfully read one field. Since you specified the
> field to be a float, it successfully read one float value.
>
> However, this may not be enough for you.
> What happens if the user types in " 1.34q" ?
> Do you want this to be an error? scanf() will return one
> here.
>
> Perhaps you should read the whole line in as a string,
> then us strtod to check for errors. Note that strtod()
> returns a double, not a float.
>
>


Thanks Fred. I'm learning among other things that I need to post below.
In essence, what I need is simple: Enter a dollar amount such as
100.00. Verify that this float (in my definition) is not anything other
than numbers and the decimal point "." I guess alphabeticals and
special characters is out of the question. So I then calculate another
amount using this first float. For example:

float myVar = 0;

printf ("\nEnter amount: ");
scanf("%f", &myVar);

I would then have to verify that this variable (myVar) does not contain
letter and special characters, in other words, positive numbers and
decimals. I tried using isdigit, but it only works with character type
variables. I am looking into creating an array of type float and then
maybe verify this way.

 
Reply With Quote
 
Xancatal
Guest
Posts: n/a
 
      10-23-2006

Bill Medland wrote:

> Xancatal wrote:
>
> > Thanks for your response Bill. Do you mean read as integer and then
> > parse it?

> No, you don't parse it. if you've read it then the characters have already
> been parsed; it's now an int.
> > How about converting it then to float? would that be
> > possible? Do you have maybe an example I can go by?
> >
> > I wast thinking I could use getchar to capture the stream from the
> > float (or the user), do the verification (you know... No weird
> > characters like $&# and so on as well as no letters), and then
> > calculate my float vars. Waddaya think?

>
> My first preference would be simply
> if (scanf("%d", &myint) != 1)
> and accept that some input will be unused.
> If I wanted to be more resilient I'd probably use fgets() and strtol().
>


Thanks Bill. I'm not familiar with fgets and strtol. I think fgets is
for file reading? In any case, what I have is something a lot more
simpler. Is only a matter of letting a user enter a dollar amount, and
letting the program determine if it is a dollar amount to do some
calculation, or otherwise prompt an error.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
converting exponential number to float number ruds Java 3 01-10-2007 10:15 PM
format a float number with specific number of decimal point Oscar Lok Ruby 1 11-19-2006 05:59 PM
how can i verify if a number has mantissa??? fzmaster C Programming 2 11-25-2005 05:36 AM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments