473,289 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

verify float number

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

Oct 23 '06 #1
43 6486
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
Oct 23 '06 #2
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>

Oct 23 '06 #3
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
Oct 23 '06 #4
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>
Oct 23 '06 #5
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.)

Oct 23 '06 #6

"Xancatal" <pe**********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr 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
Oct 23 '06 #7
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
Oct 23 '06 #8

Fred Kleinschmidt wrote:
"Xancatal" <pe**********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr 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.

Oct 23 '06 #9

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.

Oct 23 '06 #10
Fred Kleinschmidt wrote:
>
"Xancatal" <pe**********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr 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.
No it won't (or am I missing something). It will read in 1.34 and then the
q will sit around waiting for the next call to scanf and mess that up..
>
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>
--
Bill Medland
Oct 23 '06 #11
Xancatal wrote:
>
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.
Ah. Now we're getting somewhere.
In that case I would definitely use fgets(mystr,sizeof(mystr), stdin)), look
carefully at the string and parse out the leading and trailing digits (look
up strchr).
If it's dollar amounts I probably wouldn't use float either; I would use an
integer and count in cents.
--
Bill Medland
Oct 23 '06 #12
Bill Medland wrote:
Fred Kleinschmidt wrote:
>"Xancatal" <pe**********@gmail.comwrote in message
>>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.

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.

No it won't (or am I missing something). It will read in 1.34 and
then the q will sit around waiting for the next call to scanf and
mess that up..
Which is exactly what you want. The termination char remains in
the stdin stream, you can check it as you please. This includes
'\n', so you can confidently flush the remainder of the input line
if you wish:

char flushln(FILE *f) {
int ch;
while (('\n' != (ch = getc(f)) && (EOF != ch)) continue;
return ch;
}

In general scanf is fairly well behaved when you restrict it to a
single input. The confusion becomes rampant when you want it to
detect multiple fields.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Oct 23 '06 #13
CBFalconer wrote:
Bill Medland wrote:
>Fred Kleinschmidt wrote:
>>"Xancatal" <pe**********@gmail.comwrote in message
>>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.

No it won't (or am I missing something). It will read in 1.34 and
then the q will sit around waiting for the next call to scanf and
mess that up..

Which is exactly what you want. The termination char remains in
the stdin stream, you can check it as you please. This includes
'\n', so you can confidently flush the remainder of the input line
if you wish:
Ah: "scanf() will return one (the value 1)", not "scanf() will return one,
an error". I guess we talked past each other.

--
Bill Medland
Oct 24 '06 #14
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
Read in a string, Error check it, then convert it.
Oct 24 '06 #15
Xancatal wrote:

<snip>
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,
no no no! This is your fundamental misunderstanding. Take a deep
breath.
Most of the checking you want to do ***is done by scanf()***. myVar is
a float
it *cannot* hold letters or special characters. If you give scanf() a
correctly
formatted floating point number it will store the corresponding
floating point
value in myVar. If you give it something else it won't. scanf() makes
it difficult
to recover from errors, so (as others have suggested) use fgets() the
sscanf().
Once you have a floating point value you check for negative values or
out of range.

<snip>

--
Nick Keighley

Oct 24 '06 #16
Neil 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
Read in a string, Error check it, then convert it.
Thanks Neil, how do you think I could convert it? I tried this and it
failed during compile time:
float myVar = 0.0;
....
....
....

scanf ("%c", &otherVar);

myVar = &otherVar;

It looks as although I tried to assign the value of this string to the
float, it fails compilation. It says it can not assign value.

Oct 24 '06 #17

Nick Keighley wrote:
no no no! This is your fundamental misunderstanding. Take a deep
breath.
Most of the checking you want to do ***is done by scanf()***. myVar is
a float
it *cannot* hold letters or special characters. If you give scanf() a
correctly
formatted floating point number it will store the corresponding
floating point
value in myVar. If you give it something else it won't. scanf() makes
it difficult
to recover from errors, so (as others have suggested) use fgets() the
sscanf().
Once you have a floating point value you check for negative values or
out of range.

<snip>

--
Nick Keighley
Thanks Nick. In other words, if scanf does the checking, is it possible
then to use scanf or another function to check and make sure only
integers and a decimal point is allowed to be entered? If so, I think
this would solve my problem, because checking for negative number is
taken care of using a while loop.

Oct 24 '06 #18
Xancatal wrote:
>
Nick Keighley wrote:
>no no no! This is your fundamental misunderstanding. Take a deep
breath.
Most of the checking you want to do ***is done by scanf()***. myVar is
a float
it *cannot* hold letters or special characters. If you give scanf() a
correctly
formatted floating point number it will store the corresponding
floating point
value in myVar. If you give it something else it won't. scanf() makes
it difficult
to recover from errors, so (as others have suggested) use fgets() the
sscanf().
Once you have a floating point value you check for negative values or
out of range.

<snip>

--
Nick Keighley

Thanks Nick. In other words, if scanf does the checking, is it possible
then to use scanf or another function to check and make sure only
integers and a decimal point is allowed to be entered? If so, I think
this would solve my problem, because checking for negative number is
taken care of using a while loop.
No (but do you want to be that restrictive).
Is the user allowed to enter 12e3 for 12000? scanf will allow that.
--
Bill Medland
Oct 24 '06 #19

Bill Medland wrote:

Thanks Nick. In other words, if scanf does the checking, is it possible
then to use scanf or another function to check and make sure only
integers and a decimal point is allowed to be entered? If so, I think
this would solve my problem, because checking for negative number is
taken care of using a while loop.
No (but do you want to be that restrictive).
Is the user allowed to enter 12e3 for 12000? scanf will allow that.
--
Bill Medland
Bill,

Yes. In fact, I have it down to where it does not allow for negative,
or alphabets. However, the challenge now is to make sure no 12e3 or any
hybrids are allowed. In essence, to make sure only numbers and one
decimal, for example 100 or 100.00 but not -120 or 123ea2.00

Oct 24 '06 #20
"Xancatal" <pe**********@gmail.comwrites:
[...]
Thanks Neil, how do you think I could convert it? I tried this and it
failed during compile time:
float myVar = 0.0;
...
...
...

scanf ("%c", &otherVar);
The "%c" format expects a pointer to the first element of a character
array. With no length specified, the length defaults to one. A
single character object, in this context, acts like an array of length
one. You didn't show us the declaration of otherVar, but for this to
work, it should be of type char. So this is a (somewhat convoluted)
way to read a single character.
myVar = &otherVar;

It looks as although I tried to assign the value of this string to the
float, it fails compilation. It says it can not assign value.
&otherVar is the address of your char variable, so it's of type char*
(pointer-to-char). You can't assign a char* to a float; it doesn't
even make sense to try.

Part of the problem is that there are multiple ways to do what you're
trying to do.

One of the most general ways to handle text input is:

Use fgets() to read a whole line at a time into a string. Note
that the resulting string will usually contain the '\n' character
that terminated the line. (If the input line is longer than your
buffer, or if you hit end-of-file or an error, things can get more
complicated.)

Use sscanf(), or perhaps some more specific function like
strtol(), to extract the data from the string.

You *can* use scanf(), but it combines reading input and parsing it
("parsing" means analyzing data in a sequence of characters, either a
string or an input file), and it doesn't respond well to errors.
Using fgets() and sscanf() separately, if sscanf() fails, you can
easily discard the entire line, or try again with a different format.
scanf() or fscanf() is less flexible; if there's an error, it's
already consumed some amount of input.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 24 '06 #21
"Nick Keighley" <ni******************@hotmail.comwrites:
no no no! This is your fundamental misunderstanding. Take a deep
breath. Most of the checking you want to do ***is done by
scanf()***.
scanf is flawed to an extent that it is barely useful, because it
yields undefined behavior if out-of-range data is input. See C99
7.19.6.2p10:

...if the result of the conversion cannot be represented
in the object, the behavior is undefined.

I am amazed that the committee did not fix this in C99.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Oct 24 '06 #22
In article <11**********************@h48g2000cwc.googlegroups .com>
Xancatal <pe**********@gmail.comwrote:
>... 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.
If you really want to scan "dollar amount"s, you might find some
code I have lying around here useful.

The comp.lang.c folks can do their usual work on this to improve
it. :-)

(I have removed some parts that are not relevant, but left others
in since I have little time to make this posting. If I had more
time I could post a shorter article. Some additional editing will
be required to make this compile.)

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

static const char whitespace[] = " \t";

static void read_line(char *, int, char *);
static int getdouble(char **, double *, int);

int
read_input(char *fname, FILE *in)
{
int c;
char *p;
int lineno = 0;
char line[BUFSIZ];

while ((p = fgets(line, sizeof line, in)) != NULL) {
lineno++;
p = strchr(p, '\n');
if (p == NULL) {
/* not the best, but ... */
printf("%s:%d: overlong input line truncated\n",
fname, lineno);
while ((c = fgetc(in)) != EOF && c != '\n')
continue;
} else
*p = '\0';
read_line(fname, lineno, line);
}
return 0; /* XXX should return error count? */
}

/*
* Break the line into tokens, then act on them.
* This is a pretty cheesy lexer/parser but what the heck.
* We use the date parser to make sure the first (assumed to be date)
* field is sensible.
*/
static void
read_line(char *fname, int lineno, char *line)
{
char *p, *ep;
unsigned char c;
[snippage]
int rv;
double n2;

p = line;
p += strspn(p, whitespace);
if (*p == 0 || *p == '#')
return; /* ignore blank/comment lines */
ep = p;
[snippage -- code that extracted date, record type, etc]
if (getdouble(&ep, &n2, 1)) {
printf("%s:%d: malformed %s -- bad $ value\n",
fname, lineno, "line");
return;
}
[snippage]
}

#define COMMA ','
/*
* Rather stupid little algorithm to clean up "money style" numbers --
* replaces removed commas with trailing whitespace (ugh, but what
* the heck).
*/
static int
cleanmoney(char *st, char **epp)
{
size_t m;
char *t, *en;
enum { MAYBE, YES, NO } state;

en = *epp;
t = memchr(st, '.', en - st);
if (t != NULL) {
if (memchr(t + 1, COMMA, en - t - 1) != NULL)
return -1;
/* ok, no commas past decimal point */
} else
t = en;
m = t - st;
state = MAYBE;
while (m 3) {
/*
* there are at least 4 (more) digits; t points past
* the last of a group of 3 (incl eg to decpt or '\0'):
*
* "1,234.50"
* t
*
* Back it up 4 to point at the "possible comma".
* Reject if the 3 digits are a comma; then check/set
* state for comma-eating.
*/
t -= 4;
if (t[1] == COMMA || t[2] == COMMA || t[3] == COMMA)
return -1;
if (state == MAYBE)
state = t[0] == COMMA ? YES : NO;
if (t[0] == COMMA && state != YES)
return -1;
if (t[0] != COMMA && state == YES)
return -1; /* must be consistent */
if (state == YES) {
/* remove this comma */
memmove(t, t + 1, en - t - 1);
*--en = ' '; /* whack in a blank and shorten */
}
m -= 3;
}
*epp = en;
return 0;
}

/*
* Break off whitespace-separated token and turn it into a double
* via strtod. Allow "dollar" values (prefix $, embedded commas)
* if directed.
*
* Return 0 if that seemed to work.
*/
static int
getdouble(char **pp, double *result, int dollars)
{
char *cp, *ep, *ep2;
unsigned char c;

cp = *pp;
cp += strspn(cp, whitespace);
c = *cp;
if (dollars && c == '$')
c = *++cp;
if (!isdigit(c))
return -1;
ep = cp + strcspn(cp, whitespace);
if (dollars && cleanmoney(cp, &ep))
return -1;
*result = strtod(cp, &ep2);
if (ep != ep2)
return -1;
*pp = ep;
return 0;
}
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Oct 24 '06 #23
Bill Medland wrote:
Xancatal wrote:
>>
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.
Ah. Now we're getting somewhere.
In that case I would definitely use fgets(mystr,sizeof(mystr), stdin)),
look carefully at the string and parse out the leading and trailing digits
(look up strchr).
If it's dollar amounts I probably wouldn't use float either; I would use
an integer and count in cents.
Something like this.
(You'll still have to tidy it up but hopefully it will give you enough to
get you going.)
/*
* Read in an amount in a very restrictive format
*/
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
int main (int argc, char **argv)
{
/* I'm not proud of it but it should give Xancatal some pointers */
char buffer[20];
long cents = -1;
fprintf(stdout, "Enter an amount\n");
if (fgets(buffer, sizeof(buffer), stdin))
{
const char *p;
/* Skip over leading spaces, tabs etc. */
for (p = buffer; isspace(*p); p++)
;
if (isdigit(*p))
{
const char *first_digit;
const char *period;
first_digit = p;
for (; isdigit(*p); p++)
;
if (*p == '.')
{
period = p;
p++;
for (;isdigit(*p); p++)
;
if (*p == '\0')
{
fprintf(stderr, "Oh dear, the buffer was too small\n");
}
else if (*p != '\n')
{
fprintf(stderr, "Invalid additional input %s\n", p);
}
else if (p-period != 3)
{
fprintf(stderr, "Expected two figures after the
point\n");
}
else
{
char *end1;
char *end2;
cents = 100 * strtol(first_digit, &end1, 10) +
strtol(period+1, &end2, 10);
assert(end1 == period);
assert(end2 == p);
assert(cents >= 0);
}
}
else if (*p == '\0')
{
fprintf(stderr, "Oh dear, the buffer was too small\n");
}
else if (*p != '\n')
{
fprintf(stderr, "Invalid additional input %s\n", p);
}
else
{
char *end;
cents = 100 * strtol(first_digit, &end, 10);
assert(end == p);
assert(cents >= 0);
}
if (cents >= 0)
fprintf(stdout, "You entered %ld cents\n", cents);
}
else if (*p == '\0')
{
fprintf(stderr, "Oh dear, the buffer was too small\n");
}
else
{
fprintf(stderr, "Invalid input %s\n", p);
}
}
else if (ferror(stdin))
perror("Error reading input");
else
{
assert(feof(stdin));
fprintf(stderr, "No input\n");
}
return 0;
}
--
Bill Medland
Oct 24 '06 #24
/*
* Read in an amount in a very restrictive format
*/
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
int main (int argc, char **argv)
{
/* I'm not proud of it but it should give Xancatal some pointers */
Bill,

Thanks for the code. I was unable to compile it though. I get this
error:

C:\Documents and Settings\Administrator\My
Documents\dollarProject\registersNevada\test.c: line 16: incompatible
types in initializer
'long cents = -1'
aborting compile

Any clues?

Oct 25 '06 #25
av
On 23 Oct 2006 08:56:42 -0700, 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

you have just to write the right getl_() for get the line
and sscanf_m like sscanf (that return error if oveflow too)

// se ritorna 1=valore preso in val
// altrimenti valore non preso
int
get_double(double* val, double min, double max, FILE_m* pf)
{char a[512];
int k, i=0, cf1;
la0:;
k=getl_m(a, 512, pf); cf1=cf();
if(k==0){if(cf1==1)
{laa:; printf("\n"); return 0;}
printf("Errore linea troppo lunga"); goto la2;
}
if( (k=sscanf_m(a, "%f", val))!= 1 )
if(cf1==1) goto laa;
if(k!=1||*val<min||*val>max)
{la1:;
printf("Errore o importo fuori range");
la2:;
if(++i>=3) goto laa;
printf(": Riprova ");
goto la0;
}
return 1;
}

usage:
double val;
printf("Inserisci il valore ");
if( get_double(&val, 0.0, 12.0, stdin) )
return 0;

Oct 25 '06 #26
av wrote:
On 23 Oct 2006 08:56:42 -0700, 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.

you have just to write the right getl_() for get the line
and sscanf_m like sscanf (that return error if oveflow too)

// se ritorna 1=valore preso in val
// altrimenti valore non preso
int
get_double(double* val, double min, double max, FILE_m* pf)
{char a[512];
int k, i=0, cf1;
la0:;
k=getl_m(a, 512, pf); cf1=cf();
if(k==0){if(cf1==1)
{laa:; printf("\n"); return 0;}
printf("Errore linea troppo lunga"); goto la2;
}
if( (k=sscanf_m(a, "%f", val))!= 1 )
if(cf1==1) goto laa;
if(k!=1||*val<min||*val>max)
{la1:;
printf("Errore o importo fuori range");
la2:;
if(++i>=3) goto laa;
printf(": Riprova ");
goto la0;
}
return 1;
}

usage:
double val;
printf("Inserisci il valore ");
if( get_double(&val, 0.0, 12.0, stdin) )
return 0;
congratulations. In only 20 lines of code you've used more gotos
than I've used in my entire career.
--
Nick Keighley

Soon I moved onto the hard stuff - computed goto. Lord, that was cool.
I
could jump to any of a dozen places with one line of code. Talk about
POWER!!
Richard Heathfield "goto: Just Say No."

Oct 25 '06 #27
Xancatal wrote:
>
>/*
* Read in an amount in a very restrictive format
*/
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
int main (int argc, char **argv)
{
/* I'm not proud of it but it should give Xancatal some pointers */

Bill,

Thanks for the code. I was unable to compile it though. I get this
error:

C:\Documents and Settings\Administrator\My
Documents\dollarProject\registersNevada\test.c: line 16: incompatible
types in initializer
'long cents = -1'
aborting compile

Any clues?
Yes.
Either use int rather than long or use -1L for the constant.
--
Bill Medland
Oct 25 '06 #28
Bill Medland <bi*********@shaw.cawrites:
Xancatal wrote:
>>
>>/*
* Read in an amount in a very restrictive format
*/
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
int main (int argc, char **argv)
{
/* I'm not proud of it but it should give Xancatal some pointers */

Bill,

Thanks for the code. I was unable to compile it though. I get this
error:

C:\Documents and Settings\Administrator\My
Documents\dollarProject\registersNevada\test.c: line 16: incompatible
types in initializer
'long cents = -1'
aborting compile

Any clues?
Yes.
Either use int rather than long or use -1L for the constant.
No.

The declaration

long cents = -1;

is perfectly legal. The constant 1 has type int. Applying the unary
"-" operator yields a result of type int, with the obvious value. The
expression is used to initialize an object of type long, so it's
implicitly converted from int to long.

A diagnostic (warning message) is allowed, as it is for literally
anything, but it would be silly in this context, and I don't know of
any compiler that would issue a warning here. A compiler that rejects
the declaration either is badly broken, or is not a C compiler.

Something else is going on here, and changing "-1" to "-1L" is not
going to fix it (though it might conceivably work around it).

I compiled the code myself, and it compiled with no errors or
warnings. Perhaps Xancatal is doing something odd (using something
other than a C compiler, or compiling something other than the posted
source), but I have no idea what. If Xancatal can give us some more
details, we might be able to help.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 25 '06 #29
I compiled the code myself, and it compiled with no errors or
warnings. Perhaps Xancatal is doing something odd (using something
other than a C compiler, or compiling something other than the posted
source), but I have no idea what. If Xancatal can give us some more
details, we might be able to help.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith,

Thanks for your insight. I'm using a toy compiler as you can see. I
will try this out with a better compiler. I have Sun Studio 11 in
another system, but is quite complicated to compile and run simple
programs, for it develops more robust software. Does anyone know of a
better C compiler (perhaps a freebee?)

As soon as I get the compiler, I will re-try this code. I get all kinds
of weird errors when I try to compile this code as it is. :(

Oct 26 '06 #30
Xancatal wrote:
>>I compiled the code myself, and it compiled with no errors or
warnings. Perhaps Xancatal is doing something odd (using something
other than a C compiler, or compiling something other than the posted
source), but I have no idea what. If Xancatal can give us some more
details, we might be able to help.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Keith,

Thanks for your insight. I'm using a toy compiler as you can see. I
will try this out with a better compiler. I have Sun Studio 11 in
another system, but is quite complicated to compile and run simple
programs, for it develops more robust software
What's complicated about cc filename.c?

--
Ian Collins.
Oct 26 '06 #31

Ian Collins wrote:
>
What's complicated about cc filename.c?

--
Ian Collins.
I'm using the GUI Ian.

Oct 26 '06 #32
Xancatal said:
>
Ian Collins wrote:
>>
What's complicated about cc filename.c?

I'm using the GUI Ian.
I sympathise. GUIs do make computers much harder to use, don't they?
Fortunately, they are not always compulsory.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 26 '06 #33
In article <11**********************@m7g2000cwm.googlegroups. com>,
Xancatal <pe**********@gmail.comwrote:
>What's complicated about cc filename.c?
>I'm using the GUI Ian.
Doctor, it hurts when I do this...

-- Richard

Oct 26 '06 #34
Richard Heathfield wrote:
Xancatal said:
>Ian Collins wrote:
>>>
What's complicated about cc filename.c?

I'm using the GUI Ian.

I sympathise. GUIs do make computers much harder to use, don't
they? Fortunately, they are not always compulsory.
Well put. I prefer to tell the machine what to do rather than to
have it give me permission to do limited things.

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

Xancatal wrote:
Ian Collins wrote:
What's complicated about cc filename.c?

I'm using the GUI Ian.
If you find the GUI hard to use, why on earth do you use it? Why not
just follow Ian's comprehensive instructions?

Oct 27 '06 #36

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
So basically, you need to verify that an input is a properly formed,
positive floating point value.

The easy way would be to read the input as a string with fgets(), then
use strtod() to both convert it to a double value and to verify that
the input is valid, and then test that it's positive:

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

#define MAXBUF ... // large enough to hold largest input

int main(void)
{
char inBuf[MAXBUF+1];
char *endInputChar;
double inValue;

printf("Gimme a number: ");
fflush(stdout);

if (fgets(inBuf, sizeof inBuf, stdin) != NULL)
{
inValue = strtod(inBuf, &endInputChar);
/**
* endInputChar points to the first character in the inBuf
* string that is *not* converted to a floating-point
* value by strtod(). If this character is whitespace or 0,
* the string is a valid float. If the character is anything
* else, then the string is not a valid float.
*/
if (isspace(*endInputChar) || *endInputChar != 0)
{
if (inValue 0.0)
{
printf("\"%s\" is a valid, positive floating-point
value\n", inBuf)
}
else
{
printf("\"%s\" is not a positive-valued floating-point
value:\n",
inBuf, );
}
}
else
{
/**
* strtod() encountered an invalid character, which
* is pointed to by endInputChar.
*/
printf("\"%s\" is not a valid floating-point number: bad
character '%c'\n",
inBuf, *endInputChar);
}
}
else
{
printf("Error occurred while reading standard input\n");
}

return 0;
}

The slightly harder way is to write a simple lexer that scans the
string one character at a time and changes state based on the
character. It's a bit more work (enough so that I won't provide an
example at this time; maybe later when I'm not supposed to be getting
real work done), but it allows you to constrain the input to your
specific needs (for example, you may not want to allow input such as
1.0e3, which the above code will convert without complaint).

Oct 27 '06 #37
On 25 Oct 2006 01:01:24 -0700, "Nick Keighley"
<ni******************@hotmail.comwrote:
<in sig>
Soon I moved onto the hard stuff - computed goto. Lord, that was cool.
I
could jump to any of a dozen places with one line of code. Talk about
POWER!!
Richard Heathfield "goto: Just Say No."
Maybe a dozen places, or even more, but only as many as you can write
out in one statement, and anyone can easily find the list in a single
place, even a reviewer or maintainer or other such troublemaker.

Now, _assigned_ goto, that's the ticket. And usually with only little
effort you can trash the bits and go to places that aren't labels, nor
even statements, and if you're lucky nor even instructions.

<G!>

- David.Thompson1 at worldnet.att.net
Nov 20 '06 #38
>On 25 Oct 2006 01:01:24 -0700, "Nick Keighley"
><ni******************@hotmail.comwrote:
<in sig>
>Soon I moved onto the hard stuff - computed goto. Lord, that was cool.
I
could jump to any of a dozen places with one line of code. Talk about
POWER!!
Richard Heathfield "goto: Just Say No."
In article <po********************************@4ax.com>,
Dave Thompson <da*************@worldnet.att.netwrote:
>Maybe a dozen places, or even more, but only as many as you can write
out in one statement, and anyone can easily find the list in a single
place, even a reviewer or maintainer or other such troublemaker.

Now, _assigned_ goto, that's the ticket. And usually with only little
effort you can trash the bits and go to places that aren't labels, nor
even statements, and if you're lucky nor even instructions.

<G!>
Alas, C lacks assigned goto (although GNUC provides it, with
the unary && operator to take the address of labels).

For similar destructive power, there is always COBOL with its ALTER
statement.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 20 '06 #39
Chris Torek said:

<snip>
>
Alas, C lacks assigned goto (although GNUC provides it, with
the unary && operator to take the address of labels).
setjmp comes pretty close, doesn't it?
For similar destructive power, there is always COBOL with its ALTER
statement.
....and its prohibition almost invariably appears on page 1 of any COBOL
in-house coding standards document. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 20 '06 #40
>Chris Torek said:
><snip>
>Alas, C lacks assigned goto (although GNUC provides it, with
the unary && operator to take the address of labels).
In article <Yr******************************@bt.com>,
Richard Heathfield <in*****@invalid.invalidwrote:
>setjmp comes pretty close, doesn't it?
Close enough to make a big mess, certainly.
>For similar destructive power, there is always COBOL with its ALTER
statement.
>...and its prohibition almost invariably appears on page 1 of any COBOL
in-house coding standards document. :-)
One might wish for the same with regard to setjmp(). Or perhaps
longjmp() -- setjmp() without longjmp() is quite harmless. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 20 '06 #41
In article <Yr******************************@bt.com>,
Richard Heathfield <in*****@invalid.invalidwrote:
>Alas, C lacks assigned goto (although GNUC provides it, with
the unary && operator to take the address of labels).
>setjmp comes pretty close, doesn't it?
In the few circumstances I've encountered where you would want an
assigned goto - the obvious one is a "threaded code" virtual machine -
setjmp() is useless, and the usual approach is to use a big switch
statement instead. If you could rely on your compiler to do
tail-call optimisation, you might be able to use function calls.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Nov 20 '06 #42
Chris Torek said:
>>Chris Torek said:
<snip>
>>For similar destructive power, there is always COBOL with its ALTER
statement.
>>...and its prohibition almost invariably appears on page 1 of any COBOL
in-house coding standards document. :-)

One might wish for the same with regard to setjmp(). Or perhaps
longjmp() -- setjmp() without longjmp() is quite harmless. :-)
IME, setjmp/longjmp *are* often deprecated or forbidden in in-house coding
standards docs. Alas, this doesn't seem to have much effect on people. They
say something like "Yes, I know the style doc says don't use longjmp,
*but*....", and then leave others to debug the resulting pigs' breakfast.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 20 '06 #43
Dave Thompson <da*************@worldnet.att.netwrote:
On 25 Oct 2006 01:01:24 -0700, "Nick Keighley"
<ni******************@hotmail.comwrote:
<in sig>
Soon I moved onto the hard stuff - computed goto. Lord, that was cool. I
could jump to any of a dozen places with one line of code. Talk about
POWER!!
Richard Heathfield "goto: Just Say No."

Maybe a dozen places, or even more, but only as many as you can write
out in one statement, and anyone can easily find the list in a single
place, even a reviewer or maintainer or other such troublemaker.

Now, _assigned_ goto, that's the ticket. And usually with only little
effort you can trash the bits and go to places that aren't labels, nor
even statements, and if you're lucky nor even instructions.
Sinclair Basic had _proper_ computed goto. You could GOTO 2000+100*N!
That was really useful for breaking maintainability.

Richard
Nov 20 '06 #44

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bryan R. Meyer | last post by:
I am a relatively new C++ programmer and am attempting to write a function that takes a number of type float and adds commas to it in the appropriate places. In order to manipulate the number to...
0
by: Support | last post by:
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40"> <head>...
2
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in...
2
by: Chris | last post by:
Hi The following code is giving strange results........... float fTest = 536495.61f; cout << _T("float: ") << fTest << endl; int dec, sign; char* pszTest = _fcvt(fTest, 10, &dec, &sign);...
2
by: Wayne Wengert | last post by:
I want to write a Windows application to go through all the email addresses in an SQL Server table and to report which ones are invalid. From Googling and perusing NGs it is my understanding that...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
7
by: Partho | last post by:
I have a float variable which I need to add to a short variable. How do I do this? Do I have to typecast or is there a way around? I tried typecasting the float to a short, but that gives me a 0 or...
7
by: DirtyRasa | last post by:
Here is what my professor told me to to. Write a function that converts a string to a float and returns a float. Test your function by entering f4 and with 4f. Both should say Data entered was...
2
by: alishaikhji | last post by:
I am working on a program which will need several different integer and float random numbers at different stages, for example: - At one point, I need a random number (float) in the range 0.1 to 10.0...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.