problem with isdigit() and isspace()

So, I've only begun to learn C++ through a book and wanted to make a good old classic calculator for experimentation's sake. However, I had problems with creating a function to determine whether the user-inputted equation is valid. So I made this short code and ran it to test things out :
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cctype>
using namespace std;


int main(void){
    char equationStore[50];
    int indexer=0;
    do {
        equationStore[indexer]='0';   
        indexer=indexer+1;
    } while(indexer<50);
    cout << "Enter the equation :" << "\n";
    cin.getline(equationStore,50);
    indexer=0;
    do
    {
        if (equationStore[indexer]=='+')
            cout << "equationStore has + as item number " << indexer+1 << "\n";
        if (equationStore[indexer]=='-')
            cout << "equationStore has - as item number " << indexer+1 << "\n";
        if (equationStore[indexer]=='x')
            cout << "equationStore has x as item number " << indexer+1 << "\n";
        if (equationStore[indexer]=='X')
            cout << "equationStore has X as item number " << indexer+1 << "\n";
        if (equationStore[indexer]=='*')
            cout << "equationStore has * as item number " << indexer+1 << "\n";
        if (equationStore[indexer]=='/')
            cout << "equationStore has / as item number " << indexer+1 << "\n";
        if (isdigit(equationStore[indexer])==true)
        {
            cout << "equationStore has the number " << equationStore[indexer+1];
            cout << " as item number " << indexer << "\n";
        }
        if (isspace(equationStore[indexer])==true)
        {
            cout << "equationStore has whitespace as item number " << indexer+1;
            cout << "\n";
        }
        indexer=indexer+1;
    } while (indexer<50);
    int exitPar;
    cin >> exitPar;
    return 0;    
}

(I wanted to let the user use 'x', 'X', or '*' for multiplication, hence why they're in this short test program)

As it turns out, the problem is with the <cctype> functions isdigit() and isspace(). Even if there are numbers and spaces when I entered the equation, the program won't recognize it.

For example, I entered "2 + 4" as the equation when prompted, but instead of the following expected output :
1
2
3
4
5
6
7
Enter the equation :
2 + 3
equationStore has the number 2 as item number 1
equationStore has whitespace as item number 2
equationStore has + as item number 3
equationStore has whitespace as item number 4
equationStore has the number 3 as item number 5


it produced only this instead :
1
2
3
4
Enter the equation :
2 + 3
equationStore has + as item number 3
equationStore has X as item number 34


I've understood that the "equationStore has X as item number 34" part may be due to junk data, because I didn't initialize the string. But can anyone help tell me why isdigit() and isspace() isn't working properly?
isdigit() and isspace() return zero if the input is not a digit or space, respectively, or non-zero otherwise (they return int). Note that for the purposes of bool-to-int conversion, true is 1 and false is 0. However, the C++ standard says that the boolean true condition is equal to !false,
whereas false == 0. Hence, any non-zero value is true.

When you are testing == true, you are testing against 1 (due to above bool-to-int conversion). However, as the function documentation says, it returns non-zero is the condition is true.

In other words, the correct way to write the line is:

 
if( isdigit( ... ) )


and

 
if( isspace( ... ) )


As a general rule, you should never compare against true like this:

 
if( someCondition == true )


due to the bool-to-int conversion rule. It is always better to write

 
if( someCondition )


Because false is zero and zero is false, it is ok to write

 
if( someCondition == false )


however I have always preferred

 
if( !someCondition )

It works! Thank you very much! I'll keep that in mind, and will never compare to true again.
Topic archived. No new replies allowed.