What's the easiest way to check if an argument entered at the command
line is a number?
I know that one way would be to treat the argument as a character
string, and then manually check each character to make sure it is 0
through 9, with the first digit not being 0.
But this seems like such basic functionality, I'm wondering if I'm
missing something. 15 1615
On Jan 28, 3:29 pm, "H." <hbe...@gmail.c omwrote:
What's the easiest way to check if an argument entered at the command
line is a number?
I know that one way would be to treat the argument as a character
string, and then manually check each character to make sure it is 0
through 9, with the first digit not being 0.
But this seems like such basic functionality, I'm wondering if I'm
missing something.
What is your definition of a number? Does it have to be an integer or
can it be a decimal number? Can the number be negative? Can it be of
arbitrary size of does it need to fall into the range of a specific
type? Can it start with space characters? What exactly are you
trying to do?
You might find the strto* functions helpful, otherwise you will need
to clarify your definition of a number.
Robert Gamble
"H." <hb****@gmail.c omwrites:
What's the easiest way to check if an argument entered at the command
line is a number?
I know that one way would be to treat the argument as a character
string, and then manually check each character to make sure it is 0
through 9, with the first digit not being 0.
But this seems like such basic functionality, I'm wondering if I'm
missing something.
Why not leading zeros? You can try strspn:
#include <stdio.h>
#include <string.h>
int main (void)
{
char s[] = "33455678990054 31234";
char valid[] = "0123456789 ";
size_t res = strspn (s, valid);
if ( res != strlen (s) )
printf("Invalid argument: %s\n", s);
return 0;
}
--
espen
H. wrote:
What's the easiest way to check if an argument entered at the command
line is a number?
I know that one way would be to treat the argument as a character
string, and then manually check each character to make sure it is 0
through 9, with the first digit not being 0.
But this seems like such basic functionality, I'm wondering if I'm
missing something.
Best bets: strtol(), strtoul(), or strtod(), depending on
the kind of "number" you want to accept. Note that these will
accept some forms of "number" that you might not want: digit
strings with leading spaces, for example, or with a leading
minus sign. But in addition to checking for digits (and such),
they'll also check the range of the converted value: a string
of a thousand consecutive nines is "numeric," but you might not
want to consider it a "number."
Another possibility is sscanf(), but I feel the strtoxx()
functions are more direct.
--
Eric Sosman es*****@acm-dot-org.invalid
On Jan 28, 3:52 pm, Eric Sosman <esos...@acm-dot-org.invalidwrot e:
H. wrote:
What's the easiest way to check if an argument entered at the command
line is a number?
I know that one way would be to treat the argument as a character
string, and then manually check each character to make sure it is 0
through 9, with the first digit not being 0.
But this seems like such basic functionality, I'm wondering if I'm
missing something. Best bets: strtol(), strtoul(), or strtod(), depending on
the kind of "number" you want to accept. Note that these will
accept some forms of "number" that you might not want: digit
strings with leading spaces, for example, or with a leading
minus sign. But in addition to checking for digits (and such),
they'll also check the range of the converted value: a string
of a thousand consecutive nines is "numeric," but you might not
want to consider it a "number."
Another possibility is sscanf(), but I feel the strtoxx()
functions are more direct.
The scanf functions are almost always a poor choice for converting
numbers because if the number is outside the range of the expected
type it will always invoke undefined behavior.
Robert Gamble
On Jan 29, 8:04 am, "Robert Gamble" <rgambl...@gmai l.comwrote:
On Jan 28, 3:52 pm, Eric Sosman <esos...@acm-dot-org.invalidwrot e:
H. wrote:
What's the easiest way to check if an argument entered at the command
line is a number?
Best bets: strtol(), strtoul(), or strtod(), depending on
the kind of "number" you want to accept. ...
Another possibility is sscanf(), but I feel the strtoxx()
functions are more direct.
The scanf functions are almost always a poor choice for converting
numbers
You mean programmers almost always make poor choices of the way
they invoke scanf.
because if the number is outside the range of the expected
type it will always invoke undefined behavior.
No. For example, consider: int i, r = scanf("%4d", &i);
--
Peter
>What's the easiest way to check if an argument entered at the command
>line is a number?
Define "a number".
>I know that one way would be to treat the argument as a character string, and then manually check each character to make sure it is 0 through 9, with the first digit not being 0.
You forgot the leading minus sign. And perhaps leading whitespace.
And perhaps a leading 0 or 0x signifying the base.
>But this seems like such basic functionality, I'm wondering if I'm missing something.
strtol() and family will return in the pointer pointed at by its
second argument a pointer to the next character after the number
(if one was found). If that pointer is not null and points at a
string terminator, it's a valid number. If that pointer is not
null and points at characters such as a space, tab, comma, newline,
or whatever, it may or may not be something you consider valid. If
it points at something else wierd, like an underscore, it's probably
not valid.
On Jan 28, 5:22 pm, "Peter Nilsson" <a...@acay.com. auwrote:
On Jan 29, 8:04 am, "Robert Gamble" <rgambl...@gmai l.comwrote:
On Jan 28, 3:52 pm, Eric Sosman <esos...@acm-dot-org.invalidwrot e:
H. wrote:
What's the easiest way to check if an argument entered at the command
line is a number?
Best bets: strtol(), strtoul(), or strtod(), depending on
the kind of "number" you want to accept. ...
Another possibility is sscanf(), but I feel the strtoxx()
functions are more direct.
The scanf functions are almost always a poor choice for converting
number.
You mean programmers almost always make poor choices of the way
they invoke scanf.
The usefulness of scanf for converting numbers is quite limited if you
don't want to risk invoking undefined behavior. Tell me, what is the
proper way to invoke scanf to convert an int whose value may be any of
those that an int can represent without invoking undefined behavior
upon encountering a value that is not?
because if the number is outside the range of the expected
type it will always invoke undefined behavior.
No. For example, consider: int i, r = scanf("%4d", &i);
Well in that case the number being converted isn't outside the range
of the expected type now is it?
Robert Gamble
"H." <hb****@gmail.c omwrote in message
news:11******** *************@h 3g2000cwc.googl egroups.com...
What's the easiest way to check if an argument entered at the command
line is a number?
I know that one way would be to treat the argument as a character
string, and then manually check each character to make sure it is 0
through 9, with the first digit not being 0.
But this seems like such basic functionality, I'm wondering if I'm
missing something.
First, you have to realize something about both users and numbers -- they
ARE out to get you. You can't turn your back on them, or they will conspire
and move against you. It is the same principle when bowling -- the 8 and 9
pins are shielded from your view by the other pins and they are conspiring
against you.
The safest approach to the problem you're describing is to write your own
function that maps from (string) to (valid, number). Proceed in two phases:
a)Force the string into the allowed character set (i.e. it should be only
digits).
b)Use a bit of FSA theory to parse the string. You need to have a firm idea
of what constitutes a "number" for your application, and it is then easy to
devise an automaton approach to parse it.
Generally, expect to write about 100 lines of code. In goes the string.
Out comes an error/success code and a number.
--
David T. Ashley (dt*@e3ft.com) http://www.e3ft.com (Consulting Home Page) http://www.dtashley.com (Personal Home Page) http://gpl.e3ft.com (GPL Publications and Projects)
"H." wrote:
>
What's the easiest way to check if an argument entered at the
command line is a number?
I know that one way would be to treat the argument as a character
string, and then manually check each character to make sure it is
0 through 9, with the first digit not being 0.
But this seems like such basic functionality, I'm wondering if
I'm missing something.
A simple sequence to use is:
int i;
if (1 != sscanf(argv[i], "%d", &i)) puts("not a number");
else {
carryonjack();
}
provided you have first ensured that argv[i] exists by checking the
value of argc.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: - ions |
last post by:
I have created a JComboBox with its Items as a list of "M" numbers ie.
M1,M2,M3.......throgh too M110 (thes are the messier objects, a
catolouge of deep sky objects) the user selects of of these and views
it aswell as infomation. The program also has a JTextFiels which
allows the user to enter the M number. The problem i have is checking
that...
|
by: William Payne |
last post by:
Hello, I am in the process of converting a C++ program to a C program. The
user of the program is supposed to supply an integer on the command line and
in the C++ version of the program I was using something called stringstreams
to do the conversion. Here's my C version, can I leave it as it is or does
it need to be robustified or changed in...
|
by: Marlene Stebbins |
last post by:
I am entering numbers into my program from the command line. I want to
check whether they are > INT_MAX. Sounds simple, but I've discovered that
if(x <= INT_MAX)
{
/* use x in some calculation */
}
else
{
/* exit with error message */
|
by: Hulo |
last post by:
In a C program I am required to enter three numbers (integers) e.g. 256
7 5 on execution of the program.
C:\> 256 7 5
There should be spaces between the three numbers and on pressing
"enter", further processing is done. The problem requires me to check
whether three numbers have actually been entered in the input line and
to warn if less...
|
by: LSW |
last post by:
I'm using Borland Turbo C++ 3.0 to develop an embedded system to shift
data around a network. At the moment we receive a string of bytes over a
serial line and reassemble them into floating point values.
If the bytes are not assembled correctly then it's possible to produce
some floating point values that aren't 'genuine' numbers. Does...
| |
by: jacob navia |
last post by:
We hear very often in this discussion group that
bounds checking, or safety tests are too expensive
to be used in C.
Several researchers of UCSD have published an interesting
paper about this problem.
http://www.jilp.org/vol9/v9paper10.pdf
Specifically, they measured the overhead of a bounds
|
by: H.S. |
last post by:
Hello,
I am trying out a few methods with which to test of a given number is
practically zero. as an example, does the following test correctly if a
given number is zero within machine precision? I am trying out this
method to check for a practical zero in an algorithm I am implementing
in C++.
-------------------------
#include...
|
by: AndyB |
last post by:
I have found a lot of material on removing duplicates from a list, but I
am trying to find the most efficient way to just check for the existence
of duplicates in a list. Here is the best I have come up with so far:
CheckList = for x in self.__XRList]
FilteredList = filter((lambda x:x != 0),CheckList)
if len(FilteredList)...
|
by: Aaron Hsu |
last post by:
Hey all,
After seeing the Secure version I/O functions thread, it occured to me
that maybe not everyone agrees with the almost universal adage that I
have heard. I have Always been told that using things like strlcpy and
other explicitly bounded functions were better than using the
non-bounded versions like strcpy.
Is this a matter of...
|
by: ningxin |
last post by:
Hi, i am currently taking a module in c++ in the university, and was given an assignment. because i have no prior background on the subject, everything is kind of new to me. i have tried for quite some time and still not able to get the solution out. so i hope you guys can help me out. of course i am not expecting a full solution, but i would...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |