Returning Operator to Switch Statement
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Returning Operator to Switch Statement

  1. #1
    Join Date
    Jul 2012
    Location
    Kathmandu
    Posts
    31

    Returning Operator to Switch Statement

    Greeting Codeguru world! Namaste!

    I planned to build a four function fraction calculator adding some advance feature like error checking in input so that user are prompted to input correct numbers and operator.
    I created a separate member function getOper() function for getting correct operator and getFrac() function for getting correct fraction.

    My intention was just to get correct operator from getOper() function and returning the operator to switch in main() function to do specific calculation. But, i don't know why i am getting repeatedly error when inputting correct operator while debugging program.

    Any suggestion would be appreciated.
    Here's my complete code.
    Code:
    #include <iostream>
    
    using namespace std;
    char c;
    class fraction
    {
        private :
            int num;
            int oper;
            int din;
        public :
            fraction():num(1),din(1){}
            fraction(int n, int d) : num(n),din(d){}
            void getFrac();
            int getOper();
            void showData(){cout << num << c << din << endl;}
            fraction operator +(fraction);
            fraction operator -(fraction);
            fraction operator *(fraction);
            fraction operator /(fraction);
    };
    
    void fraction::getFrac()
    {
       while(true)
        {
            cout <<"\nEnter Fraction in (N/D) Format : ";
            cin.unsetf(ios::skipws);
            cin >> num >> c >> din;
            if(din==0)
            {
                cout <<"\nDenominator cannot be 0";
                cin.clear(ios::failbit);
            }
            if(cin.good())
            {
                cin.ignore(10,'\n');
                break;
            }
            cin.clear();
            cin.ignore(10,'\n');
            cout <<"\nIncorrect Input! Use Appropriate Numerical Value";
        }
    }
    
    int fraction::getOper()
    {
        while(true)
        {
            cout <<"\nEnter Operator ('+','-','*','/'): ";
            cin.unsetf(ios::skipws);
            cin >> oper;
            if(oper!='+'||oper!='-'||oper!='*'||oper!='/')
            {
                cout <<"\nError! Use Valid Operator";
                cin.clear(ios::failbit);
            }
            if(cin.good())
            {
                cin.ignore(10,'\n');
                break;
            }
            cin.clear();
            cin.ignore(10,'\n');
        }
        return oper;
    }
    
    fraction fraction :: operator +(fraction f1)
    {
        int di,nu;
        nu = num + f1.num;
        di = din + f1.din;
        return fraction(nu,di);
    }
    fraction fraction :: operator -(fraction f1)
    {
        int di,nu;
        nu = num - f1.num;
        di = din - f1.din;
        return fraction(nu,di);
    }
    fraction fraction :: operator *(fraction f1)
    {
        int di,nu;
        nu = num * f1.num;
        di = din * f1.din;
        return fraction(nu,di);
    }
    fraction fraction :: operator /(fraction f1)
    {
        int di,nu;
        nu = num * f1.din;
        di = din * f1.num;
        return fraction(nu,di);
    }
    
    int main()
    {
        char ch;
        fraction f1, f2, op, result;
        do
        {
            f1.getFrac();
            op.getOper();
            f2.getFrac();
            switch(op.getOper())
            {
                case '+' : result = f1+f2; break;
                case '-' : result = f1-f2; break;
                case '*' : result = f1*f2; break;
                case '/' : result = f1/f2; break;
            }
            cout <<"\nResult is "; result.showData();
            cout <<"\nDo Another ? (y/n): "; cin >> ch;
        }while(ch=='y');
        return 0;
    }
    Regards
    Basanta

    Note : I checked the program again by assigning char to variable "oper" and function "getOper(). But, result is same.And again i changed variable "oper" to default keyword operator, but, it really didn't worked as expected.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,228

    Re: Returning Operator to Switch Statement

    if(oper!='+'||oper!='-'||oper!='*'||oper!='/')

    Think about that. If oper = +, it is not = -, therefore it's always true.

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: Returning Operator to Switch Statement

    Quote Originally Posted by basanta View Post
    Any suggestion would be appreciated.
    Here's my complete code.
    Code:
    int fraction::getOper()
    {
        while(true)
        {
            cout <<"\nEnter Operator ('+','-','*','/'): ";
            cin.unsetf(ios::skipws);
            cin >> oper;
            if(oper!='+'||oper!='-'||oper!='*'||oper!='/')
            {
                cout <<"\nError! Use Valid Operator";
                cin.clear(ios::failbit);
            }
            if(cin.good())
            {
                cin.ignore(10,'\n');
                break;
            }
            cin.clear();
            cin.ignore(10,'\n');
        }
        return oper;
    }
    oper is an integer, so the cin will fail if the user enters valid input. Use a char instead. Also, you are asking the user to enter the operator twice.
    Furthermore:
    - Why do you have a global variable 'c'?
    - Keep input/output out of classes that have nothing to do with it. Your fraction class should not have functions that use input/output. Either overload the stream operators or use some global functions.
    - Your classes are not const-correct.
    - Your operators are not implemented correctly.
    Last edited by D_Drmmr; September 5th, 2012 at 09:22 AM.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Join Date
    Jul 2012
    Location
    Kathmandu
    Posts
    31

    Re: Returning Operator to Switch Statement

    Quote Originally Posted by GCDEF View Post
    if(oper!='+'||oper!='-'||oper!='*'||oper!='/')

    Think about that. If oper = +, it is not = -, therefore it's always true.
    Couldn't not understand. I was just trying to print the error message until user give right operator from list. I am pretty much confused can switch in main recognize the operator return from this function ?

  5. #5
    Join Date
    Jul 2012
    Location
    Kathmandu
    Posts
    31

    Re: Returning Operator to Switch Statement

    Quote Originally Posted by D_Drmmr View Post
    oper is an integer, so the cin will fail if the user enters valid input. Use a char instead. Also, you are asking the user to enter the operator twice.
    Furthermore:
    - Why do you have a global variable 'c'?
    - Keep input/output out of classes that have nothing to do with it. Your fraction class should not have functions that use input/output. Either overload the stream operators or use some global functions.
    - Your classes are not const-correct.
    - Your operators are not implemented correctly.
    I used global variable for input where user input int/int in console screen and 'c' is for user choice like 2/5 or 2:5..
    Const-correct? Pardon. It mean, my classes are not constant?
    Yah, i am pretty much sure, my operator are not implemented correctly, but, i am confused how to implement it..

    Can you help me ?
    Regards
    Basanta

  6. #6
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Returning Operator to Switch Statement

    Quote Originally Posted by basanta View Post
    Couldn't not understand. I was just trying to print the error message until user give right operator from list. I am pretty much confused can switch in main recognize the operator return from this function ?
    C++ isn't the same as speaking English. Learn what boolean operators "&&" and "||" are supposed to do.

    Look carefully at the logic of your if() statement. Assume that oper is a '*'. Read your if() statement again, carefully:

    Since oper is a '*', then it is not a '+'. Since the very first part of that if() statement says "if operator is not a '+'", then guess what? That entire if() statement is true, since the very first condition is true. All of those conditions are separated with ||, so just one of those conditions being true means everything is true.

    C++ is not the same as speaking English. When we talk to each other and say "or this or that or the other", then yes, we mean all of them. But for C++ and most programming languages, the term "||" and "&&" has a specific meaning and function.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: Returning Operator to Switch Statement

    Quote Originally Posted by basanta View Post
    I used global variable for input where user input int/int in console screen and 'c' is for user choice like 2/5 or 2:5..
    You don't need to use a global variable for this; just use a local variable.
    Quote Originally Posted by basanta View Post
    Const-correct? Pardon. It mean, my classes are not constant?
    Did you search for it? The very first result returned by google gives a complete explanation.
    Quote Originally Posted by basanta View Post
    Yah, i am pretty much sure, my operator are not implemented correctly, but, i am confused how to implement it..
    First work out on paper what these functions have to do. If you have
    Code:
    n3/d3 = n1/d1 + n2/d2
    how do you express valid values for n3 and d3 in terms of n1, n2, d1 and d2? It's just calculus, it has nothing to do with programming. But until you can answer that question, there is no point in writing any code.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center