Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to compare a symbol typed in by a user with another symbol?

I’ve tried with comparing char variable with a symbol in "" and it sais:

ISO C++ forbids comparison between pointer and integer [-fpermissive]

Here is full code:

#include <iostream>

int main(){
    int a, b;
    char c;
    std::cin >> a >> c >> b;
    if (c == "+"){
        std::cout << (a + b);
    }
    if (c == "-"){
        std::cout << (a - b);
    }
    if (c == "*"){
        std::cout << (a * b);
    }
}

What should I do?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

"c" results in a character array or what we call a C-string, which is not what you want. If you want to have an expression which represents a single character use 'c' instead!

The code becomes:

#include <iostream>

int main(){
    int a, b;
    char c;
    std::cin >> a >> c >> b;
    if (c == '+'){
        std::cout << (a + b);
    }    
    if (c == '-'){
        std::cout << (a - b);
    }    
    if (c == '*'){
        std::cout << (a * b);
    }    
}

As reading the other answer here: You should decide to use string to string compare or character to character compare. Mixing it up will simply not work out of the box.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading