Thread: ISO C++ Forbids it!

  1. #1
    Jimmey
    Join Date
    Jul 2005
    Location
    Manchester, England
    Posts
    6

    ISO C++ Forbids it!

    Program wont compile because of a:
    Comparison between pointer and integer
    Can this program be saved? Is it badly written? I'm only new, so any help's much appreciated.

    ( I should probably comment my code - But, sometimes I comment not enough, sometimes I comment too much. On larger programs I comment, but on smaller ones, I think when left un-commented, they look nicer )

    Code:
    class operate{
          public:
                 char oper;
                 int w;
                 int x;
                 int recognize(char opr, int y, int z);
                 int add(int y, int z);
                 int multiply(int y, int z);
                 int divide(int y, int z);
    };
    
    int operate::add(int y, int z){
        return y + z;
    }
    
    int operate::multiply(int y, int z){
        return y * z;
    }
    
    int operate::divide(int y, int z){
        return y / z;
    }
    
    int operate::recognize(char opr, int y, int z){
        if ( opr == "+" ){
             return add(y,z);
        }
        else if (opr == "*" ){
             return multiply(y,z);
        }
        else if (opr == "/" ){
             return divide(y,z);
        }
        else {
             return 0;
        }
    }
    
    int main(){
        try {
            operate calculator;
            cout << ">>>";
            cin >> calculator.w;
            cout << ">>>";
            cin >> calculator.oper;
            cout << ">>>";
            cin >> calculator.x;
            cout << calculator.recognize(calculator.oper, calculator.w, calculator.x);
        }
        catch (...) {
              cout << "Default error. " << endl;
        }
        system("PAUSE");
    }
    Last edited by Jimmey; 12-05-2005 at 05:48 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    opr == "+"
    opr is a single char and "+" is a string.
    You need to use
    Code:
    opr == '+'

  3. #3
    Jimmey
    Join Date
    Jul 2005
    Location
    Manchester, England
    Posts
    6
    Thanks -
    Sorry to post so much code for such a simple mistake

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    main() should return a value (but it doesn't have to; 0 is returned implicitly).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 01-09-2006, 07:17 AM
  2. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 06:20 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Replies: 1
    Last Post: 11-06-2003, 08:05 PM
  5. ISO C++ forbids declaration of
    By bhorrobi in forum C++ Programming
    Replies: 4
    Last Post: 10-31-2003, 03:36 PM