473,406 Members | 2,312 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,406 software developers and data experts.

if string is a number (beginner)

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;
}
Dec 22 '06 #1
13 1774
fool wrote:
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]; i++) /* why this instead of the while? */
{
if (buffer[i] < '0' || buffer[i] '9') /* can you decode this? */
{
printf("error\n");
break;
}
}
Brian
Dec 22 '06 #2
fool wrote:
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

Dec 22 '06 #3

"fool" <fo**@fool.comwrote in message
news:MP************************@news.sunsite.dk...
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.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.
Dec 22 '06 #4
fool wrote:
>
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).

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

fool wrote:
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?

Dec 22 '06 #6
On 22 Dec 2006 00:58:53 -0800, "Scorpio" <av*******@gmail.comwrote:
>
fool wrote:
>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?
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
--
jay
Dec 22 '06 #7
"Scorpio" <av*******@gmail.comwrote:
fool wrote:
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?
Because strtol() is better - it doesn't have undefined behaviour on
overflow.

Richard
Dec 22 '06 #8
Richard Bos wrote:
"Scorpio" <av*******@gmail.comwrote:
>fool wrote:
>>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?

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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Dec 22 '06 #9
>Richard Bos wrote:
>>... strtol() is better [than atoi()] - it doesn't have undefined
behaviour on overflow.
In article <45***************@yahoo.com>
CBFalconer <cb********@maineline.netwrote:
>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.
--
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.
Dec 22 '06 #10
Chris Torek <no****@torek.netwrites:
CBFalconer <cb********@maineline.netwrote:
>>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.
I suspect it's more difficult outside the C locale.
--
"Your correction is 100% correct and 0% helpful. Well done!"
--Richard Heathfield
Dec 22 '06 #11
Chris Torek wrote:
CBFalconer <cb********@maineline.netwrote:
>Richard Bos wrote:
>>... 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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Dec 23 '06 #12
Ben Pfaff <bl*@cs.stanford.eduwrote:
Chris Torek <no****@torek.netwrites:
CBFalconer <cb********@maineline.netwrote:
>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.

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
Dec 27 '06 #13
On Fri, 22 Dec 2006 07:56:18 +0100, Ulrich Eckhardt
<do******@knuut.dewrote:
<snip other good points>
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
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
Jan 3 '07 #14

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

Similar topics

8
by: Julia Briggs | last post by:
Hello, trying to create logic tests against a string allowing someone to enter a certain character once or twice into a form consecutively, but not twice seperated between other characters.... ...
3
by: stanlo | last post by:
hi to everyone, this is still a follow up of my project ,mathematical expression.this project is meant to evaluate mathemtical expressions with oparators,+,-,*,/.more than two operands can be done,...
8
by: Nader | last post by:
Hello all, In C# string is a reference type but I learned that string is different from other reference types such as class. For example, if you pass a string argument to a method and then...
31
by: JAKE | last post by:
I'm pretty new to ansi c and I'm stuck I'm trying to assemble a string in a called function. I need to send it three different data types and return the assembled string. I've been getting errors...
2
by: zhege | last post by:
I am a beginner of C++; I have a question about the std:string and std:cout class; Two pieces of code: -------------------------------- #include <iostream> #include <string> using namespace...
87
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for...
18
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a...
5
by: SMichal | last post by:
Hi, how can I parse string "? 20.000" to double ?
3
by: Chelong | last post by:
hey now i have a problem,for me a c++ beginner.so i need ur help? here i have a text file from the copy of excel data,like this: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.