Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My ISDIGIT() Function has fallen and cant get up!!! 1

Status
Not open for further replies.

BigTeeJay

Technical User
Jun 13, 2001
106
US
Greetings,
I appears that I "broke" my isdigit() function :) I was
using it to test arguments passed to a VERY simple (or
so I thought) command line DOS program. It was originally
evaluating input... but now appears to return 0 (which
according to my book & my debugging efforts means it
wasnt an integer) regardless of what value is passed to it.

Below is example code...
Code:
#include <iostream.h>
#include <stdlib.h>
#include <ctype.h>

int main(int argc, char *argv[])
{
	//process cmd line, see whats returned, currently always a 0 (zero)
	cout << endl;
	cout << &quot;ATOI (v): &quot; << atoi( argv[1] ) << endl;
	cout << &quot;ISDIG(v): &quot; << isdigit( atoi( argv[1] ) ) << endl << endl;

	//process a fixed string, see what happens (returns 0 for me)
	char *tstStr = &quot;3&quot;;
	cout << &quot;ATOI (s): &quot; << atoi( tstStr ) << endl;
	cout << &quot;ISDIG(s): &quot; << isdigit( atoi( tstStr ) ) << endl << endl;

	//pass a freaking INT, which isdigit still says isnt a digit!?!?!?
	int tstInt = 3;
	cout << &quot;VALUE(i): &quot; << tstInt << endl;
	cout << &quot;ISDIG(i): &quot; << isdigit( tstInt ) << endl << endl

	return 0;
}

...in desparation I even commented out the &quot;endl&quot;'s
being passed to cout, thinking there was some remote
chance that these were being evaluated by the isdigit
function too (I know... but, like I said... desparate).

This is driving me nuts!!!!!!! What am I missing here?
I am sure this is going to be something I am going to
want to crawl under a rock when someone points out an
blatent miss... but until then, I cant find it for the
life of me.

Regards,
Tj
 
You misunderstand the isdigit(int). The argument here is an integer. If it is between '0' and '9', i.e, 48-57, it will return a non-zero integer, otherwise it will return 0.
try isdigit('3') to see what it returns.
 
isdigit is decieving. What it is really looking for is an ascii value so '0' '1' etc will work and in the case of

char *tstStr = &quot;3&quot;;

if you call

isdigit(tstStr[0]) you should get a true value

Matt
 
argggggg..... I started thinking about that
last night, when I was looking at the code
and thought that digit doesnt necessarily
mean int... but was too tired to think straight :)

I thank you both for your assistance!

Regards,
Tj
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top