-
September 5th, 2012, 07:17 AM
#1
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.
-
September 5th, 2012, 08:11 AM
#2
Re: Returning Operator to Switch Statement
if(oper!='+'||oper!='-'||oper!='*'||oper!='/')
Think about that. If oper = +, it is not = -, therefore it's always true.
-
September 5th, 2012, 08:20 AM
#3
Re: Returning Operator to Switch Statement
Originally Posted by basanta
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 08: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
-
September 6th, 2012, 02:12 AM
#4
Re: Returning Operator to Switch Statement
Originally Posted by GCDEF
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 ?
-
September 6th, 2012, 02:27 AM
#5
Re: Returning Operator to Switch Statement
Originally Posted by D_Drmmr
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
-
September 6th, 2012, 05:15 AM
#6
Re: Returning Operator to Switch Statement
Originally Posted by basanta
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
-
September 6th, 2012, 10:15 AM
#7
Re: Returning Operator to Switch Statement
Originally Posted by basanta
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.
Originally Posted by basanta
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.
Originally Posted by basanta
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|