error: ISO C++ forbids comparison between pointer and integer

Well I'm not exactly sure what the error means, but it's
error: ISO C++ forbids comparison between pointer and integer [-f permissive]


Here's my code, the program is just a basic console-based calculator. I'm still learning C++ :P
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
46
47
#include <iostream>

using namespace std;

int main()
{
    char method[1];
    cout << "Please enter a method of calculation (*,/,+,-)";
    cin.getline(method,1);

    if (method > 1)
    {
        cout << "You must enter a valid calculation method";
    }

    if (method[1] == "*")
    {
        int fnumbM;
        int snumbM;
        cout << "Enter first number: ";
        cin >> fnumbM;
        cout << "Enter second number: ";
        cin >> snumbM;
        int answerM;
        answerM = fnumbM * snumbM;
        cout << answerM <<endl;
        return 0;
    }

    if (method[1] == "*")
    {
        int fnumbD;
        int snumbD;
        cout << "Enter first number: ";
        cin >> fnumbD;
        cout << "Enter second number: ";
        cin >> snumbD;
        int answerD;
        answerD = fnumbD * snumbD;
        cout << answerD <<endl;
        return 0;

    }

    return 0;

}


The bold text is where I received errors. I noticed it's whenever I'm using the IF statement to test my variable, but I'm not sure exactly what I did wrong though.
Last edited on
Line 11 is trying to see if the address of your array method is greater than 1, which is what the error is about. If you want to actually compare the first character of that array, use method[0].

The leads to the problems on line 16 and 20; method[1] is the second element of the array, which doesn't exist as you declared the array with only one element. Use method[0] instead.

This could be avoided by simply using a char instead of an array though.
Just an idea to show how you might avoid repeating code - here is some psuedo code:

1
2
3
4
5
//1 get a number
//2 get an operator
//2.1 check it's a valid operator
//3 get a number
//4 calculate answer 


This shows which parts could be functions, items 1 & 3 are the same, so you could make it a function that is called twice, or you can combine them by getting both numbers like you have, but make it a function.

Always keep a look out for repeated code - it means you need a function or a loop.

You can make use of a switch statement with a default clause for the IsOperator function, and to do the calculation.

Also be aware of what happens with integer division, not only do you have to check for division by zero, but realise that 2 / 5 is zero because of truncation.

When checking for equality with zero for a float or double, don't use the == operator, just check whether the absolute value is less than some arbitrary precision like 0.001 say. This is because FP numbers are stored as binary fractions, and cannot represent every real number. To see how things can go wrong print out the value of double 0.1 to 16 decimal places.


HTH
Last edited on
By the way your array method will always contain symbol '\0' because it is defined as having only one element and getline( method, 1 ) will store only terminating zero.


1
2
3
    char method[1];
    cout << "Please enter a method of calculation (*,/,+,-)";
    cin.getline(method,1);


You should declare method as at least

char method[2];

and use getline as

cin.getline( method, 2 );
Last edited on
Topic archived. No new replies allowed.