Check if a number is a digit or not

Hello, I'd like to write a function that checks to see if a number is a digit or not. This is as far as I've gotten, and the program just won't seem to run

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h"
#include "iostream"
#include "ctype.h"
using namespace std;

char isDigit(int x)
{	
	if (isDigit(x))
		return 1;
	else 
		return 0;


}
int _tmain(int argc, _TCHAR* argv[])
{
	int x;
	int y;
	cout << "Please press a key" << endl;
	cin >> x;
	isdigit(x);
	y = isdigit(x);
	cout << y;
First,

you have a function char isDigit(int x); which is not used (note that these are case, senstive)

Second, x should be a char type, not an int.

Third, is digit will return true or false, so it doesn't really make sense to cout y directly.

Something like this would work:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <ctype.h>
using namespace std;

int main(int argc, char* argv[])
{
	char x;
	int y;
	cout << "Please press a key" << endl;
	cin >> x;

	cout << x << " is" << (isdigit(x)?"":" not") << " a digit" << endl;
}
1
1 is a digit
Press any key to continue...

a
a is not a digit
Press any key to continue...
I have a few questions for you.
Where in your isDigit(int) function are you checking to see if x is a digit? And you're calling the function recursively, and it will loop infinitely.
Why is isDigit(int) returning a char? Wouldn't it make more sense for it to return a boolean value?

In main, you make no reference to the function you created.

Also, in ctype.h, there is already a function called isdigit(int) that returns 0 if the digit isn't an int.
1
2
if(isdigit(x))
    cout << "x is a digit." << endl;

There's no real need to make your own isDigit function.

EDIT:
I find Stewbond's description much more helpful than mine, lol.
Last edited on
I'm trying to get it to work as a function though. Like, outside of main. I tried this but same problem - program won't run.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "stdafx.h"
#include "iostream"
#include "ctype.h"
using namespace std;

char isDigit(char x)
{	
	if (isDigit(x))
		return 1;
	else 
		return 0;


}
int _tmain(int argc, _TCHAR* argv[])
{
	char x;
	int y;
	cout << "Please press a key" << endl;
	cin >> x;
	isDigit(x);
	cout << isDigit(x);
	
}
Last edited on
1
2
3
4
5
6
char isDigit(char x)
{	
	if (isDigit(x)) // <---- You're calling your own function again. 
		return 1;
	else 
		return 0;

You're calling your own function recursively, which results in an indefinite loop that you'll never get out of.
I think what you mean to use is the function isdigit(x) (that's defined in ctype.h.)
Bear in mind that C++ is case sensitive.
isdigit(x) is not the same thing as isDigit(x).
Last edited on
If you use the function from <ctype.h> then there's no need to write your own.

Alternatively, you could do something like
1
2
3
4
bool is_digit( char c)
{
    return (c>='0' && c<='9');
} 
Ok, I tried this - I'm trying to return 1 and 0 for true/false in the function, to go to main. But the program doesn't cout 1 or 0 at the end. Just trying to make sure it works, then I'll set up something like 'if 1 do this, else do that'.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "stdafx.h"
#include "iostream"
#include "ctype.h"
using namespace std;

char Digit(char x)
{	
	if (isdigit(x))
		return 1;
	else 
		return 0;


}
int _tmain(int argc, _TCHAR* argv[])
{
	char x;
	int y;
	cout << "Please press a key" << endl;
	cin >> x;
	Digit(x);
	cout << Digit(x);
	
	
}
Last edited on
You are using a return type of char. If you want the printable characters 1 and 0, then put them in single quotes, like any other character constant.

Alternatively, return a boolean (much like my example). The default when using cout with a bool type is to print '1' or '0'. So that might be cleaner and more appropriate, since you really want a true/false outcome, (just two possibilities) while a char type allows for 256 different possibilities.
Last edited on
Ok, so if it was int then I wouldn't need to put quotes?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "stdafx.h"
#include "iostream"
#include "ctype.h"
using namespace std;

bool Digit(char x)
{	
	if (isdigit(x))
		return 1;
	else 
		return 0;


}
int _tmain(int argc, _TCHAR* argv[])
{
	char x;
	int y;
	cout << "Please press a key" << endl;
	cin >> x;
	Digit(x);
	cout << Digit(x);
	
	
}


I did that, and it works. At least, it seems to. Do you see any more problems with it?
As you said, you could use an int rather than a char.

Not a problem exactly, but the if/else is unnecessary here:
1
2
3
4
5
6
7
bool Digit(char x)
{	
	if (isdigit(x))
		return 1;
	else 
		return 0;
}


You could simply have this:
1
2
3
4
bool Digit(char x)
{	
	return isdigit(x);
}

which would achieve the same result.
Thanks for all the help!
Topic archived. No new replies allowed.