473,698 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

checking for numbers

H.
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 1628
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
Jan 29 '07 #11
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
Jan 29 '07 #12
H.
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?

Jan 29 '07 #13
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
Jan 29 '07 #14
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

Jan 29 '07 #15
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
Jan 30 '07 #16

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

Similar topics

7
7579
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...
5
11064
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;
2
3708
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 */
7
2633
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.
3
3701
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...
125
6559
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
4
7919
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>
1
3324
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.
27
2286
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?
21
11368
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...
0
8674
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, 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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8604
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,...
0
9157
Oralloy
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...
0
9028
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8861
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 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...
0
7728
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4369
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...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.