Find the answer to your Linux question:
Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By alf55
Ok, I have posted about this same program but it was made in python. Now I am working on the same thing except i C++ and I am having a ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Sep 2012
    Posts
    148

    C++ Calculator Pointer help


    Ok, I have posted about this same program but it was made in python. Now I am working on the same thing except i C++ and I am having a harder time. Maybe it is because I am trying to code it like I did in python but its not working. I have tried messing with the syntax and I keep ending up in the same place and I don't know where else to go from here. So here is the code:
    Code:
    #include <iostream>
    using namespace std;
    
    class equation{
        private:
        int num1;
        char ope;
        int num2;
        float awnser;
        public:
            int calculation()
            {
                cout << "Enter in your first number: ";
                cin >> num1;
                cout << "Enter in your Operator: ";
                cin >> ope;
                cout << "Enter in your second number: ";
    
                if(ope == "+"){
                    awnser = num1 + num2;
                }
                if(ope == "-"){
                    awnser = num1 - num2;
                }
                if(ope == "*"){
                    awnser = num1 * num2;
                }
                if(ope == "/"){
                    awnser = num1 / num2;
                }
                if(ope == %){
                    awnser = num1 % num2;
                }
                cout << "the awnser is" << awnser << endl;
                return awnser;
            }
    
    
    
    
    };
    
    int main(void)
    {
        equation CalcObject;
        CalcObject.calculation();
        return 0;
    }
    The error message says I can't compare numbers an pointers and I am assuming that it means that I cant compare numbers and operators but I don't know how else I am supposed to do that. THANK YOU!!!

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    630
    In C and C++ there is a big difference between single and double quotes. A (non-class string uses the double qoute (examples: "this is a string", "a") and they are a pointer to the first character of the string and the string has an extra character ( chr(0) at the end to say when it ends). While a character literal uses the single quote (examples 'a', 'b') which is a single character. So you were correct that it was because you were attempting to code as you could in python where there is no real difference in the two quotes.
    awesomepilot likes this.

  3. #3
    Linux Newbie
    Join Date
    Sep 2012
    Posts
    148
    OHHHHHHH ok see I would have never guessed that. It works now though. Thank You!! So when comparing variables in classes use single quotes otherwise if its just in a function use double quotes?

  4. #4
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    630
    The string class uses the the double quotes (the storage is different inside the class as it has storage, length, etc). The single quote is for a single char which is not a string but a character.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •