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.

RegardsCode:#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; }

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.