Hi. I wonder why this doesn't work.
I expected to see ch1 and ch3 are numbers but it shows they are operators....
How can I make this work?

#include <iostream>
using namespace std;

void isItNumber( char *ch )
{
    if( strcmp( ch, "*" ) || strcmp( ch, "/" ) || strcmp( ch, "%" ) || strcmp( ch, "+" ) || strcmp( ch, "-" ) )
    {
        cout << "ch is an operator\n" << flush;
    }
    else
    {
        cout << "ch is a number\n" << flush;
    }
}

int main()
{
    char *ch1  = "123", *ch2 = "*", *ch3 = "-123", *ch4 = "-";

    cout << ch1 << " " << ch2 << " " << ch3 << " " << ch4 << endl;

    isItNumber( ch1 );
    isItNumber( ch2 );
    isItNumber( ch3 );
    isItNumber( ch4 );

    return 0;
}

The strcmp function returns 0 when the strings are the same, i.e., you should do:

if( ( strcmp( ch, "*" ) == 0 ) || 
    ( strcmp( ch, "/" ) == 0 ) || 
    ( strcmp( ch, "%" ) == 0 ) || 
    ( strcmp( ch, "+" ) == 0 ) || 
    ( strcmp( ch, "-" ) == 0 ) )

The reason for this is that strcmp can also be used to alphabetically sort strings (it returns a negative number if alphabetically "less-than", and vice versa).

oh, the strcmp condition is wrong..
you can write

if( strcmp( ch, "*" )==0 || strcmp( ch, "/" )==0 || strcmp( ch, "%" )==0 || strcmp( ch, "+" )==0 || strcmp( ch, "-" )==0 )

so, you have strcmp(op1, op2) if is ==0 that means op1 and op2 is equal

This program is clearly intended to test the function, but it would be helpful to know the overall goal, that is, what the program this test is intended to prepare for is supposed to do. Is this a parsing project for a compiler course, by any chance? It has the feel of the 'convert infix to prefix and compute on the stack' project typically given around week two or three in such courses.

Two questions come to mind: one, is there a reason you are testing a whole string against single characters, and two, is there a reason you cannot use the C++ string class rather than C-strings?

If my guess about the purpose of this is correct, I would advise against testing for the operator as a string and instead tokenize the string character-by-character instead. Trying to read the source string in as a whole and comparing it to the operators will fail for a number of kinds of legitimate input, such as:

"2*6"    // no separation between the tokens

"1   * 6"  // extra whitespace in the token

What you would want to do is read the characters one at a time, and if the character is a number (you can use isdigit() for that), you collect the characters until you find one that isn't a number. You can do similarly for variable names, if you are supporting them in your calculator; use isalpha() to tes tthe first character, then if it is an identifier, collect anything that is a letter, a number, or an underscore, until you find something that doesn't match. Save the values and the type of token in a Token struct or class, along these lines:

enum TokenType {INVALID, INTEGER, IDENTIFIER, ADDOP, MULOP, ASSIGNOP};

struct Token 
{
    std::string value;
    TokenType type;
};

You'll want to have the addition and subtraction operators in one group and the multiplication, division and modulo in another, because they have different precedences in infix notation. You can bet the instructor will have you do precedences later on if this assignment doesn't cover it already.

BTW, you might want to look into CppUnit for creating your unit tests in the future. It takes more work, but it gives you a structured way of performing and repeating the tests without interfering with the program being tested.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.