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.
Jan 28 '07
15 1632
Gordon Burditt wrote:
[Gordon, it might amuse you to see what Thunderbird's spell-
checker does with your surname.]
>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.
Therein lies a user interface trap. If you specify zero
as the base for strtol() or its brethren, they will interpret
a leading 0 or 0x as indicating base eight or sixteen. If a
program accepts input from humans who are not C programmers
this is almost certainly a Bad Thing: Ordinary people expect
leading zeroes to be place-holders and nothing more. If you
use strtoul() this way to accept telephone numbers in three
parts, for example, you'll have trouble with 781 424 0116: it
will be accepted, but when you dial you'll reach 781 424 0078.
Recommendation: "Almost always" specify ten explicitly as
the base for strtoxx(). The extra flexibility gained by using
zero is "almost always" unwanted and potentially harmful.
--
Eric Sosman es*****@acm-dot-org.invalid
Eric Sosman wrote:
Gordon Burditt wrote:
[Gordon, it might amuse you to see what Thunderbird's spell-
checker does with your surname.]
I had to try it myself, that truly was amusing ;)
Jeff
int i;
if (1 != sscanf(argv[i], "%d", &i)) puts("not a number");
else {
carryonjack();
}
But aren't you assuming here that the digits are all 1s?
In article <11************ **********@k78g 2000cwa.googleg roups.com>,
H. <hb****@gmail.c omwrote:
>
> int i;
if (1 != sscanf(argv[i], "%d", &i)) puts("not a number"); else { carryonjack(); }
But aren't you assuming here that the digits are all 1s?
No, sscanf() returns the -number- of successfully matched items,
0 if no items were successfully matched, and EOF if there is
end of input string before the first character.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
On Jan 28, 9:24 pm, CBFalconer <cbfalco...@yah oo.comwrote:
"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.
A better solution would be to use one of the strto* functions as they
don't invoke UB if the number is too large and allow you to more
easily determine if the data after the conversion is valid. For
example, the string "123abc" will be deemed a number by the presented
sscanf solution whereas strtol can set a pointer to the "a" in the
string which the program can use to examine the data after the number
and determine if it is valid.
Robert Gamble
Jeffrey Stedfast <st******@comca st.netwrote:
Eric Sosman wrote:
Gordon Burditt wrote:
[Gordon, it might amuse you to see what Thunderbird's spell-
checker does with your surname.]
I had to try it myself, that truly was amusing ;)
I don't have an up-to-date Thunderbird here, but let me guess: Gordon
Bennett!?
Richard 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 what the user has entered is valid, that being an M followed by 1
- 110 Nothing else, i thought of...
|
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 any manner, regarding error
checking?
char* endptr;
errno = 0;
|
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 or more numbers have been entered.
|
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 anyone
have any suggestions on how to check a float/double is valid (i.e.
checking for NANs, infinity...
| |
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 <iostream>
#include <iomanip>
|
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) len(sets.Set(FilteredList)): return
False
The first statement pulls the slice out of a matrix I need to check.
|
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 good programming style, or is this just needless overhead?
|
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 greatly appreciate it if anyone can suggest to me what should i do. i am very new to this subject so...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
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.
| |