testing operators as literals

I would like to know why the following code fails to recognize '-' or '+' as operators but does recognize '*' and '/'. The following code is supposed to check the input for one of four mathematical operators: -, +, * or /.

When I input a - or +, the test fails with the output being:

"The operator entered is: "
"You entered an incorrect oper: Try again."

When I input a * or /, the test succeeds with the output being:

"The operator entered is: "
"The operator is: *" OR "The operator is: /" depending on which is input

Can someone tell me why '-' and '+' are not recognized in the test but '*' and '/' are?

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
string checkOp(){

    double v;
    string o;
    int K = FALSE;

    while(!K){
        if(cin>>v){
            cout<<"The input was not an operator.\n";
            cout<<"The number entered is: "<<v<<"\n";
        }
        else {
            cin.clear();
            getline(cin, o);
            cout<<"The operator entered is: "<<o<<"\n";
            if(o[0] == '+' || o[0] == '-' || o[0] == '*' || o[0] == '/'){
                K = TRUE;
                cout<<"The operator is: "<<o<<"\n";
            }
            else {
                cout<<"You entered an incorrect oper: "<<o<<" Try again.\n";
            }
        }
    }
    return o;
}
+2.3 and -34.5 are read as numbers; try entering + 2.3 or - 34.5
This code is part of a larger whole, a rudimentary calculator. This part simply checks for the type of operation to perform as input by the user and so must check for the operator separately from the numbers to operate on. This is an exercise, #7 in Chapter 3, from Bjarne Stroustrup's "Programming Principles and Practices Using C++" book.

The program correctly accepts signed numbers, but doesn't correctly see '-' or '+' as operators but does for '*' and '/'.
Last edited on
As JLBorges stated: '+' and '-' are considered part of the number and causes no error hence the else branch will not be executed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
    double first ;
    char oper ;
    double second ;

    while( std::cout << "enter expression eg. 2.34 + -3.2 : " && std::cin >> first >> oper >> second )
    {
        // note: may want to special case first/0 (inf) and 0/0 (nan)
        if( oper == '+' ) std::cout << first << " + " << second << " == " << first+second << '\n' ;
        else if( oper == '-' ) std::cout << first << " - " << second << " == " << first-second << '\n' ;
        else if( oper == '*' ) std::cout << first << " * " << second << " == " << first*second << '\n' ;
        else if( oper == '/' ) std::cout << first << " / " << second << " == " << first/second << '\n' ;
        else std::cout << "unsupported operator '" << oper << "'\n" ;
    }
}
I'm not having problems with the numbers, signed with a + or a -. My problem is with the program detecting the '+' or '-' operators. I've posted the snippet as whole code below. It runs, but there are no instructions displayed onscreen. When you compile and run it, it will end when you enter either a / or a *, all other characters will be displayed with an error message. What I find is that entering the '-' or '+' characters result in blanks in the displayed output whereas all other characters are displayed as expected in the error messages.

Is there a reason that '-' and '+' are not displayed like the other characters, which would explain why they are not recognized as + and -. NOTE: these keys work! I'm typing them in this post, so something is going on in the program.

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
#include <iostream>

using namespace std;
#define TRUE 1
#define FALSE 0

int main()
{
        double v;
    string o;
    int K = FALSE;

    while(!K){
        if(cin>>v){
            cout<<"The input was not an operator.\n";
            cout<<"The number entered is: "<<v<<"\n";
        }
        else {
            cin.clear();
            getline(cin, o);
            cout<<"The operator entered is: "<<o<<"\n";
            if(o[0] == '+' || o[0] == '-' || o[0] == '*' || o[0] == '/'){
                K = TRUE;
                cout<<"The operator is: "<<o<<"\n";
            }
            else {
                cout<<"You entered an incorrect oper: "<<o<<" Try again.\n";
            }
        }
    }
    return 0;
}
Last edited on
When we have double v ; std::cin >> v ; and the user types in a + or a - as the first character, that character is consumed (it is interpreted as the leading sign of a number).
OK, that makes sense. I searched all over and hadn't found anything mentioning that fact. Thank you very much. Now I just have to figure out how to deal with it.
Topic archived. No new replies allowed.