if string is a number (beginner)

F

fool

Dear group,
I want to check if the given string is a number. If any value is
inputed other than a number then error. The following is not the correct
solution. Can some one tell me any link for the above task? Pls just
don't give the code. Thanks.

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char *got;
got = malloc(sizeof (char));
if(got == NULL)
{
got = malloc(sizeof (char));
if(got == NULL)
exit(EXIT_FAILURE);
}

scanf("%s",got);
while( (*got)++ != '\0')
{
if(*got != ('0' - '9'))
{
printf("error\n");
break;
}
else
printf("%s\n",got);
}
return 0;
}
 
D

Default User

fool said:
Dear group,
I want to check if the given string is a number. If any value is
inputed other than a number then error. The following is not the
correct solution. Can some one tell me any link for the above task?
Pls just don't give the code. Thanks.

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char *got;
got = malloc(sizeof (char));

This allocates exactly one character's worth of space. That's pretty
useless. Why are you even dynamically allocating memory?
if(got == NULL)
{
got = malloc(sizeof (char));

If it failed before, try again? Why?
if(got == NULL)
exit(EXIT_FAILURE);
}

scanf("%s",got);

Your allocated memory does not enough room to store any string longer
than the empty string. The scanf() function as used above has no way to
limit the number of characters entered. If it's longer than the empty
string, then Undefined Behavior results.
while( (*got)++ != '\0')

You dereferenced the pointer, then incremented the result. I doubt
that's what you wanted.
{
if(*got != ('0' - '9'))

What do you think that minus sign does? Whatever, what it actually does
is subract the numerical value of the two characters, which will result
in -9.

It doesn't matter, because if the loop executed at all (not the empty
string) then you had UB before, and more now.
{
printf("error\n");
break;
}
else
printf("%s\n",got);
}
return 0;
}


What book are you using?


Here's a brief snippet (the comments are for you to think about):

char buffer[80]; /* magic numbers are bad, how would you fix that? */
int i;

fgets(buffer, sizeof buffer, stdin);
/* what if more than that is entered? */

for(i = 0; buffer; i++) /* why this instead of the while? */
{
if (buffer < '0' || buffer > '9') /* can you decode this? */
{
printf("error\n");
break;
}
}


Brian
 
U

Ulrich Eckhardt

fool said:
char *got;
got = malloc(sizeof (char));
if(got == NULL)
{
got = malloc(sizeof (char));
if(got == NULL)
exit(EXIT_FAILURE);
}

This allocation looks strange. You retry without anything changing and
expect that to then yield a result - it won't happen. If a malloc failure
can't be recovered from, I'd suggest using xalloc. This is not a library
function like malloc but a wrapper around it that tries to allocate the
memory and, in case of failure, writes an error message to stderr and
exit()s. Using this helps you remove those 6 lines of error-handling code
that distracts from the real logic of the program.
Also, you only allocate space for a single character and that size is 1, by
definition (i.e. the standard says that sizeof (char)==1).
scanf("%s",got);

Bad idea, you are reading an unbounded string into an array of size 1.
Well, in fact this is a bad idea regardless of how long the array is, you
should never use unbounded function parameters. You can tell scanf how
long the target string is. Also, there is fgets() (be sure to read the
warnings for gets(), which looks more convenient at first!) which only
does input as a string.
while( (*got)++ != '\0')

This is not what you want. Try this on a loop with a normal character
literal and step through it with a debugger.
if(*got != ('0' - '9'))

Sorry, but while this looks intuitive, C doesn't let you. The '0' and '9'
are treated as integers, and the minus sign in between just yields the
difference between them. What you need is a check that the character is at
least zero and at most nine. There is also a library function isdigit(),
but firstly that is difficult to use correctly and secondly it also
includes the minus and plus signs, IIRC.

Lastly, in the while() expression, it seems like you already moved to the
next character. I suggest printing the current character as integer inside
your loop. That way you will better understand what's going on.

Uli
 
M

Malcolm

fool said:
Dear group,
I want to check if the given string is a number. If any value is
inputed other than a number then error. The following is not the correct
solution. Can some one tell me any link for the above task? Pls just
don't give the code. Thanks.

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char *got;
got = malloc(sizeof (char));
if(got == NULL)
{
got = malloc(sizeof (char));
if(got == NULL)
exit(EXIT_FAILURE);
}

scanf("%s",got);
while( (*got)++ != '\0')
{
if(*got != ('0' - '9'))
{
printf("error\n");
break;
}
else
printf("%s\n",got);
}
return 0;
}

strtol() for integers, or strtod, for floating-point numbers, are your
friend.
Unfortunately for newbies they take a pointer to a pointer to indicate the
end of the digits that can be interpreted as numbers.
However the idea is not too complicated. if the end pointer points to 0, the
end of string character, you have a valid number. If it is a non-digit, you
might want to throw the number out. If the end pointer is at the beginning
of the string, there were no digits at the beginning of the string so you
must throw the input out.
Watch for whitespace, by the way.
 
C

CBFalconer

fool said:
I want to check if the given string is a number. If any value is
inputed other than a number then error. The following is not the
correct solution. Can some one tell me any link for the above
task? Pls just don't give the code. Thanks.

You don't need any intermediate string. Just getc, together with
routines to skip blanks and the tests available in ctype.h. If you
want a demonstration see txtinput.c in txtio.zip, routine readxwd,
available at:

<http://cbfalconer.home.att.net/download/>

Some things in that zip are undergoing revision, but that routine
is solid. The changes have to do with signed input and arranging
for readxwd to follow normal unsigned behaviour for overflows. At
exit readxwd returns the termination char, so you can easily check
that is an allowable one. In particular if it is a '.' or a 'e'
(or 'E') you may have found a real value (double or float).
 
S

Scorpio

fool said:
Dear group,
I want to check if the given string is a number. If any value is
inputed other than a number then error. The following is not the correct
solution. Can some one tell me any link for the above task?

Why not just use the function atoi from<stdlib.h> ?
 
J

jaysome

Why not just use the function atoi from<stdlib.h> ?

Because with atoi() there is not a guaranteed way to distiguish
between an integer value of 0 and an invalid integer value--both may
return 0.

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
printf("atoi(\"0\") == %d\n", atoi("0"));
printf("atoi(\"A\") == %d\n", atoi("A"));
return 0;
}

Others have properly suggested the use of the strto* functions.

Happy Holidays
 
R

Richard Bos

Scorpio said:
Why not just use the function atoi from<stdlib.h> ?

Because strtol() is better - it doesn't have undefined behaviour on
overflow.

Richard
 
C

CBFalconer

Richard said:
Because strtol() is better - it doesn't have undefined behaviour on
overflow.

Not quite. strtoul won't detect the out-of-range input of "-1",
for example. It makes the mistake of incorporating the - into the
number parsing. I want to know if the user has tried to stuff
something outside the principal range into the variable.
 
C

Chris Torek

Richard said:
... strtol() is better [than atoi()] - it doesn't have undefined
behaviour on overflow.

Not quite. strtoul won't detect the out-of-range input of "-1",
for example.

This is not "out of range", by definition. By strtoul()'s definition,
admittedly. :) If this disagrees with your own definition, it is
easy enough to prohibit a minus sign.

If you want to allow '-':

char buf[N];
char *ep;
int base;
unsigned long result;
...
errno = 0;
result = strtoul(buf, &ep, base);
if (result == ULONG_MAX && errno == ERANGE)
overflow();
else if (*ep != NULL)
there_was_trailing_stuff();
else
all_is_well();

To prohibit the '-', augment the strtoul() call with, e.g.:

if (buf[strspn(buf, " \t\n\r\b\v")] == '-')
there_is_a_leading_minus();
else if ((result = strtoul(buf, &ep, base)) == ULONG_MAX && errno == ERANGE)
... as before ...

or you can check for a '-' character after calling strtoul():

char *minusp;
...
result = strtoul(buf, &ep, base);
minusp = strchr(buf, '-');
if (minusp != NULL && (ep == NULL || minusp < ep))
there_was_a_leading_minus();
else if (result == ULONG_MAX && errno == ERANGE)
... as before ...

Of course, strtoul() could have been defined to prohibit signs
(plus and/or minus), and callers could explicitly allow them; or
it could even have taken flags indicating how to treat signs; but
this is the situation we have now, so code like the above will
handle it.
 
B

Ben Pfaff

Chris Torek said:
This is not "out of range", by definition. By strtoul()'s definition,
admittedly. :) If this disagrees with your own definition, it is
easy enough to prohibit a minus sign.

I suspect it's more difficult outside the C locale.
 
C

CBFalconer

Chris said:
CBFalconer said:
Richard said:
... strtol() is better [than atoi()] - it doesn't have undefined
behaviour on overflow.

Not quite. strtoul won't detect the out-of-range input of "-1",
for example.

This is not "out of range", by definition. By strtoul()'s
definition, admittedly. :) If this disagrees with your own
definition, it is easy enough to prohibit a minus sign.
.... snip ...

Of course, strtoul() could have been defined to prohibit signs
(plus and/or minus), and callers could explicitly allow them; or
it could even have taken flags indicating how to treat signs; but
this is the situation we have now, so code like the above will
handle it.

I have written code to do just that, and input from streams (no
buffer needed). It will handle any line that a stream can supply.
I expect to modify it to use unsigned long, as that will be more
flexible eventually and the overhead is negligible. A wrapper
handles signed ints. skipwhite, unlike ignoreblanks, pushes back
the exit char. Thus ignoreblanks is static and local to this
module. The count of leading zeroes is only limited by the ability
of the stream itself. The following is an extract without
#includes.

/* -------------------------------------------------------------
* Skip to non-blank on f, and return that char. or EOF The next
* char that getc(f) will return is unknown. Local use only.
*/
static int ignoreblks(FILE *f)
{
int ch;

do {
ch = getc(f);
} while ((' ' == ch) || ('\t' == ch));
/* while (isblank(ch)); */ /* for C99 */
return ch;
} /* ignoreblks */

/*--------------------------------------------------------------
* Skip all blanks on f. At completion getc(f) will return
* a non-blank character, which may be \n or EOF
*
* Skipblks returns the char that getc will next return, or EOF.
*/
int skipblks(FILE *f)
{
return ungetc(ignoreblks(f), f);
} /* skipblks */

/*--------------------------------------------------------------
* Skip all whitespace on f, including \n, \f, \v, \r. At
* completion getc(f) will return a non-blank character, which
* may be EOF
*
* Skipwhite returns the char that getc will next return, or EOF.
*/
int skipwhite(FILE *f)
{
int ch;

do {
ch = getc(f);
} while (isspace(ch));
return ungetc(ch, f);
} /* skipwhite */

/*--------------------------------------------------------------
* Read an unsigned value. Signal error for overflow or no
* valid number found. Returns 1 for error, 0 for noerror, EOF
* for EOF encountered before parsing a value.
*
* Skip all leading blanks on f. At completion getc(f) will
* return the character terminating the number, which may be \n
* or EOF among others. Barring EOF it will NOT be a digit. The
* combination of error, 0 result, and the next getc returning
* \n indicates that no numerical value was found on the line.
*
* If the user wants to skip all leading white space including
* \n, \f, \v, \r, he should first call "skipwhite(f);"
*
* Peculiarity: This specifically forbids a leading '+' or '-'.
*/
int readxwd(unsigned int *wd, FILE *f)
{
unsigned int value, digit;
int status;
int ch;

#define UWARNLVL (UINT_MAX / 10U)
#define UWARNDIG (UINT_MAX - UWARNLVL * 10U)

value = 0; /* default */
status = 1; /* default error */

ch = ignoreblks(f);

if (EOF == ch) status = EOF;
else if (isdigit(ch)) status = 0; /* digit, no error */

while (isdigit(ch)) {
digit = ch - '0';
if ((value > UWARNLVL) ||
((UWARNLVL == value) && (digit > UWARNDIG))) {
status = 1; /* overflow */
value -= UWARNLVL;
}
value = 10 * value + digit;
ch = getc(f);
} /* while (ch is a digit) */

*wd = value;
ungetc(ch, f);
return status;
} /* readxwd */

Not too complicated. No use of errno. Straightforward for
interactive use. Follows the conventions for wrapping of unsigned
values.
 
R

Richard Bos

Ben Pfaff said:
I suspect it's more difficult outside the C locale.

I suspect not, because Chris overlooked the simple, obvious answer.

Call strtol() first. If the answer is negative, the buffer has a minus
sign or a locale-specific equivalent in it. This is safe even if the
answer is positive and too large for a signed long while fitting in an
unsigned one, because of the way in which strtol() must handle overflow
- i.e., it's not UB and, significantly, it will not wrap around to
negative.
Only if strtol()'s answer was positive or zero, call strtoul() to get
your real answer.

Richard
 
D

Dave Thompson

On Fri, 22 Dec 2006 07:56:18 +0100, Ulrich Eckhardt
Sorry, but while this looks intuitive, C doesn't let you. The '0' and '9'
are treated as integers, and the minus sign in between just yields the
difference between them. What you need is a check that the character is at
least zero and at most nine. There is also a library function isdigit(),
but firstly that is difficult to use correctly and secondly it also

I don't think it's difficult to use, although people's MMV. The only
tricky bit about the ctype.h is* and to* functions is that you must
give them a value in the range of unsigned char, or EOF (necessarily
negative and usually -1). If you are getting thevalue from a plain
char, including the common case of a string which is an array of plain
char (with a terminating null), on a system where plain char is (or
really is like) signed, that value might be negative and must be dealt
with, most simply by casting to u-char. In some cases, like this one,
a negative plain (or signed) char value can just be rejected, because
digits are in the basic charset and must be positive in plain char.
includes the minus and plus signs, IIRC.
That definitely not. ato* and strto* and also *scanf %d and i and even
u x o accept a leading sign, but isdigit and isxdigit do not.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,052
Latest member
KetoBeez

Latest Threads

Top