Teen Programmers Unite  
 

 

Return to forum top

Checking for Numeric Input

Posted by eternaldisciple [send private reply] at October 10, 2001, 08:46:14 AM

Can anyone help me think of just *how* I would check input to see if it's numeric. By the way, I'm using C++. I'll try to figure out the syntax on my own, but just some general ideas would help. Thanks.

Posted by gian [send private reply] at October 10, 2001, 03:27:57 PM

Well, I'm thinking ascii codes...
Iterate though all the digits and check if their ascii values are within the certain 0-9 range....
pretty slow and inefficient but that's just me for you.

Posted by taubz [send private reply] at October 10, 2001, 03:47:06 PM

Yeah, a number is always of the form [+-]?[0-9]*(.[0-9]*)? in terms of regular expressions. That would be fairly to write by hand to check, or if there's an RE class that you have access to, then... yeah.

Or... scanf() and check that it returns nonzero (if memory serves)? Or atoi() and related functions.

- taubz

Posted by gian [send private reply] at October 10, 2001, 05:07:20 PM

Or there is isalpha() maybe !isalpha() ?

Posted by lordaerom [send private reply] at October 10, 2001, 08:46:02 PM

isdigit() sounds better to me.

Posted by gian [send private reply] at October 10, 2001, 10:40:48 PM

That'll be what I was looking for :-)

Posted by method [send private reply] at October 17, 2001, 05:01:27 PM

Hi,

It's very easy.. Here's example in java(it's easy to port that to C++):

for( int i=0;i<string.length();i++ )
{
char ch = string.charAt( i );

if( ch >= '0' && ch <= '9' )
{
// we know that current character is digit.
}
}

- method..

Posted by lordaerom [send private reply] at October 17, 2001, 06:17:24 PM

Even Java provides nice methods for character classifications!
java.lang.Character.isDigit()!

Posted by method [send private reply] at October 20, 2001, 04:27:16 PM

Yea I know that but I just tried to simulate how to do that without using a built-in structures.

- method..

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.