How to read numbers and operators only from a string?

Hi all,

I'm trying to make a postfix/RPN calculator, and everything is working except for one little thing. If I take an input of something like "5 3f +" it returns 8, but what I want it to do is say there is a syntax error because of the f directly following the 3. To check for this, I tried to make a method that would take the whole input string, turn each number into a char array, and then search those for arrays for anything besides a number or operator.

The idea I had was to add to a counter if anything besides a number or operator is found. If the counter != 0, then the method would return false, and it would tell the user there is an operator error.

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
  bool charCheck(string _input)
{
    int counter = 0;
    istringstream check(_input);
    string word2;
    while (check >> word2)
    {
        if(word2 != "sqrt")
        {
            int length = word2.length();
            char tab2[length];
            strncpy(tab2, word2.c_str(), sizeof(tab2));
            for(int i = 0; i < length; i++)
            {
                if(tab2[i] != '0' && tab2[i] != '1' && tab2[i] != '2' && tab2[i] != '3' && ... && tab2[i] != '+' && tab2[i] != '-' && tab2[i] != '*' && tab2[i] != '/')
                {
                    counter++;
                }
                else
                {
                    counter = counter + 0;
                }
            }

            if(counter == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        else if(word2 == "sqrt")
        {
            return true;
        }
    }


The (...) in the above code represents the rest of the numbers. The program is still returning 8 if I put in "5 3f +". How do I fix this?
I don't think you need:

1
2
    istringstream check(_input);
    string word2;


_input is already a string type. If you're trying to pass over each character in _input and find a non-integer, do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <ctype.h> //gives you isdigit() function which tests if a char is a digit.

//...

for(int i = 0; i< _input.size(); i++)
{
    if(    !(isdigit(_input[i])) && (_input[i] != '+') && (_input[i] != '-') && (_input[i] != '*') && (_input[i] != '/')    )
        counter++    


    //You dont need counter = counter + 0 since it doesn't change counter value.
    //If you're not changing counter value, no point in the else statement in for loop.
}

//...



counter=counter + 0 means don't change counter. You don't need an instruction to not change something. So get rid of this since it doesn't change anything:
1
2
3
4
5
else
{
    counter = counter + 0;
}
Last edited on
Figured it out. My
1
2
3
4
if (counter == 0)
{
    return true;
}

statement was in the wrong place, I shifted it now it works. Thanks for the help!
Hi,

Just wanted to mention my hearty dislike hatred of constructs like the one below.


if(tab2[i] != '0' && tab2[i] != '1' && tab2[i] != '2' ......

They are ugly, error prone, non-scalable and did I mention UGLY :+D

Instead it would be much better to learn how to use a switch or an if - else if - else chain.

Now for some comedy:+)

Imagine you work in industry, you get a carpet call from your boss. While standing at attention on the carpet in front of his/her desk, the boss is asking wtf is that? The boss might even threaten to apply his /her boot to your rear end, in order for you to enter orbit around Jupiter !

I hope you see the funny side :+D
Last edited on
Topic archived. No new replies allowed.