<cctype>

I'm having a bit of trouble with some code.

I'm writing a function that fills an array, but when I try to terminate the program using isdigit() it doesn't work, as it only wants int's; I only want doubles.

1
2
3
4
5
6
7
8
9
10
if(!isdigit(a))
		{
			for (int j = i; j < length; j++)
			{
			   arr[j] = '/0';
			   break;

			}

		}


I was just wondering if there was any way to see if a double is alphabetic.
closed account (10oTURfi)
If you look closely, isdigit() function takes one int parameter.
It does?

a is a double, so an int is never put into it.

(From what I know anyway.)

Well, I guess this doesn't have a solution.
Last edited on
closed account (10oTURfi)
Yeah, an 8 bit integer, we call it a character.

EDIT: I was just being sarcastic in my first post. Why for the gods sake you would test if a double is a digit.
Last edited on
It was a challenge in a book I have.
doing isdigit() was the only way taught.

I could do

 
while(isdigit(a))

but again it's a double.
closed account (10oTURfi)
You still dont get it, do you? lol
There are ten digits, and they are 0 1 2 3 4 5 6 7 8 9, when used in combinations they make numbers, such as 45 , therfore digit!=number.

I guess someone has been skipping math classes :)

http://www.cplusplus.com/reference/clibrary/cctype/isdigit/
http://en.wikipedia.org/wiki/Numerical_digit

EDIT:
also read this
http://en.wikipedia.org/wiki/Number
Last edited on
I didn't ask to be patronised, I asked to be helped.

I'll make it clear for you, since you quite obviously have been skipping English classes.

I would to know whether or not it is possible to overcome the problem of using a double with isdigit(), as I would like to test whether or not it is alphabetic or any other character that isn't a number.


If you read the link you had provided, you would see that isdigit() only responds with an int.

This has nothing to do with math, it is pretty much only the language
closed account (10oTURfi)
Aaand you still dont get it.
isdigit() compares element in char array with all 10 digits, and digits are I repeat, 1 2 3 4 5 6 7 8 9 0, if it matches at least one of those characters, it will return true, else it returns false. Learn a difference between DIGIT (read: character) and number (mathematical object which represents count)
Consider this fragment:
1
2
3
4
5
int main()
{
    int a = 75;
    if(isdigit(a)) cout << "Integer is a digit." << endl;
}

Program will NOT output "Integer is a digit." beacuse it does NOT respond to int, or double or float, but only checks if an element in CHAR array is a DIGIT.

Also consider this fragment:
1
2
3
4
5
int main()
{
    char a[50] = "185asd";
    if(isdigit(a[0])) cout << a[0] << " is a digit." << endl; 
}

Element 0 in array a is "1", and its a digit, therfore it returns true.
Also your book should have told you that cctype... (let me quote)

declares a set of functions to classify and transform individual characters.

All these functions take as parameter the int equivalent of one character and return an int, that can either be another character or a value representing a boolean value: an int value of 0 means false, and an int value different from 0 represents true.


and the link I provided you says...
int isdigit ( int c );
Checks if parameter c is a decimal digit character.


EDIT: Also I repeat, your question did not make any sense. It is in many ways completly useless to check if double is a digit.
Last edited on
Right, well that's cleared up a lot from the book, thank you.

Although, I don't remember saying that I was checking if a double is a digit, in fact, I was checking for the exact opposite.


I was just wondering if there was any way to see if a double is alphabetic.


So, now that you've cleared up that cctype only responds to an array of chars, could you possibly help me out with that?
closed account (10oTURfi)
Double cannot be aphabetic. No matter what you do, you will not store alphabetic value in a double. If you try to store alphabetic value in double data type, it will not store it, instead it will store some weird number (remember: it cannot hold char no matter what)

Consider this fragment:
1
2
3
4
5
{
    double a;
    cin >> a;
    cout << a << endl;
}


It will output -9.25596e+061.

So... if you add this:
if(isalpha(a)) cout << "omg i stored alpha in a double" << endl;
I ran it, and program crashed.

EDIT: So... Since we cannot store an alpha, i am afraid that your question is invalid :( But to make it simple: NO!
Last edited on
closed account (10oTURfi)
Ow one more thing. I think I did a bad job explaining something concerning isdigit:
if integer has a value between 48 and 58, isdigit will return true, beacuse digits between 0 and 9 converted to integer represent values between 48 and 58 ^^ Try it out.
Last edited on
Thank you.

Sorry if I sounded a bit worked up earlier, a few things had happened.

Anyway, thanks for sorting out that for me, it will help me out with a lot of my code.

Topic archived. No new replies allowed.