473,378 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

need help

hello!!!
I am new to this site.can anyone help me in my program.It is of rational number class in c++ having operator overloading.It is not executing and i am not able to find mistakes.
My teacher told me that all functions in it should be cascadable.
plz plz point out my mistakes.I have to submit it on monday.




#include<iostream.h>
#include<conio.h>


enum bool { false,true};

class Rational{
int num;//numerator
int denom;//denomenator
int whl;//whole number
public:
Rational();//constructor
Rational& setRational (int a,int b, int c);//setting the rational numbers
void setNumerator(int);
void setDenominator(int);
void setwholenumber(int);
int getNumerator();
int getDenominator();
int getwholenumber();
Rational operator + (Rational &r1);//addition operator overloaded
Rational operator - (Rational & r1);//subtraction operator overloaded
Rational operator * (Rational & r1);//multiply operator overloaded
Rational operator / (Rational & r1) ;//division operator overloaded
bool operator > (Rational & r1);//greater then operator overloaded
bool operator < (Rational & r1);//less the operator overloaded
bool operator >= (Rational & r1);//greater then equal to overloaded
bool operator <= ( Rational & r1);//less then equal to overloaded
bool operator != (Rational & r1);//not equality operator overloaded
Rational& operator ++ ();//increment operator overloaded
Rational& operator -- ();//decrement operator overloaded
bool operator == (Rational & r1);//equality operator overloaded
Rational & operator += (Rational & r1);//+=overloaded
friend istream & operator >> (istream & input, Rational & r1);//friend function for input
friend ostream & operator << (ostream & output, Rational & r1);//friend function for output
Rational &simplifyRational();//simplification
~Rational();
};


/************************************************** *******************************
****************************constructor*********** ******************************************/
Rational::Rational()
{
whl=0;
num=0;
denom=1;
}
/*****************************getters and setters******************************************* *********/

void Rational::setNumerator(int n)
{
num = n;
}

void Rational::setDenominator(int d)
{
denom = d;
}
void Rational::setwholenumber(int w)
{
whl = w;
}
int Rational::getNumerator()
{
return num;
}

int Rational::getDenominator()
{
return denom;
}
int Rational::getwholenumber()
{
return whl;
}


Rational& Rational::setRational(int a,int b,int c)
{
this->whl=a;
this->num=b;
if (c==0)
this->denom=1;
else
{
this->denom=c;
this->num=(denom*whl)-num;
this->whl=num/denom;
this->denom=denom;
}
return *this;
}
/****************************arithmatic operators***************************************** ***********/

Rational Rational:: operator + (Rational &r1)
{
Rational temp;
temp.num=(num*r1.denom)+(denom*r1.num);
temp.denom=denom*r1.denom;
temp.simplifyRational();
return temp;
}
Rational Rational:: operator - (Rational & r1)
{
Rational temp;
temp.num=(num*r1.denom)-(denom*r1.num);
temp.denom=denom*r1.denom;
temp.simplifyRational();
return temp;
}
Rational Rational::operator * (Rational & r1)
{
Rational temp;
temp.num=num*r1.num;
temp.denom=denom*r1.denom;
temp.simplifyRational();
return temp;
}

Rational Rational::operator / (Rational & r1)
{
Rational temp;
temp.num= num*r1.denom;
temp.denom=denom*r1.num;
temp.simplifyRational();
return temp;
}
/**************************comparison operators***************************************** ***********/

bool Rational:: operator > (Rational & r1)
{
if (num/denom > r1.num/r1.denom)
return true;
else
return false;
}

bool Rational:: operator < (Rational & r1)
{
if (num/denom < r1.num/r1.denom)
return true;
else
return false;
}

bool Rational:: operator >= (Rational & r1)
{
if ((num/denom > r1.num/r1.denom) || ((num==r1.num) && ( denom==r1.denom)))
return true;
else
return false;
}

bool Rational:: operator <= ( Rational & r1)
{
if (( num/denom < r1.num/r1.denom)|| ((num==r1.num)&&(denom==r1.denom)))
return true;
else
return false;
}
bool Rational:: operator != (Rational & r1)
{
if ((num!=r1.num)&&(denom!=r1.denom))
return true;
else
return false;
}
/****************************increment & decrement operator****************************************** */
Rational& Rational::operator ++ ()
{
this->num=num+denom;
this->denom=denom;
return *this;
}
Rational& Rational:: operator -- ()
{
this->num=num-denom;
this->denom=denom;
return *this;
}
/******************************assignment operator****************************************** */
bool Rational:: operator == (Rational & r1)
{
if ((num==r1.num)&&(denom==r1.denom))
return true;
else
return false;
}
/************************************************** *******************************************/
Rational& Rational:: operator += (Rational & r1)
{
this->num=(num*r1.denom)+(r1.num*denom);
this->denom=denom*r1.denom;
return *this;
}
/*********************************insertion & extraction operator*****************************************/
istream & operator >> (istream & input, Rational & r1)
{
int num1,num2,num3;
input>>num1>>num2>>num3;
r1.setRational(num1,num2,num3);
return input;
}

ostream & operator << (ostream & output, Rational & r1)
{
r1.whl=r1.num/r1.denom;
r1.num=r1.num%r1.denom;
output<<r1.whl<<" "<<r1.num<<"/"<<r1.denom;
return output;
}


/***********************************simplify function****************************************** *********/
Rational& Rational::simplifyRational()
{
int largest,gcd;
if(num>denom)
largest=num;
else
{
largest=denom;
for(int i=2;i<=largest;i++)
{
if ((num%i==0)&&(denom%i==0))
gcd=i;
this->num=num/gcd;
this->denom=denom/gcd;
}
}
return *this;
}

/**********************************destructor****** ****************************************/
Rational::~Rational()
{
}
/************************************************** **************************
************************************************** ****************************/
class RatUI{
Rational r1,r2;
public:

void showmenu()
{
char ch;
cout<<"Enter two rational no.s in (a b/c) format";
cin>>r1>>r2;

do{
clrscr();
cout<<"enter the choice to proceed";
cout<<"1: to sum two rational numbers";
cout<<"2: to subtract two rational numbers";
cout<<"3: to multiply two rational numbers";
cout<<"4: to divide two rational numbers";
cout<<"5: to compare two rational numbers by '>'";
cout<<"6: to compare two rational numbers by '<'";
cout<<"7: to compare two rational numbers by '<='";
cout<<"8: to compare two rational numbers by '>='";
cout<<"9: to copmare two rational numbers by '!='";
cout<<"10: to increment first rational number";
cout<<"11: to increment second rational number";
cout<<"12: to decrement first rational number";
cout<<"13: to decrement second rational number";
cout<<"14: to exit the menu";

option(ch);

}while(ch!='14')

void option(char c)
{
switch(c)
{
case'1':cout<<r1+r2;break;
case'2':cout<<r1-r2;break;
case'3':cout<<r1*r2;break;
case'4':cout<<r1/r2;break;
case'5':cout<<r1>r2;break;
case'6':cout<<r1<r2;break;
case'7':cout<<r1<=r2;break;
case'8':cout<<r1>=r2;break;
case'9':cout<<r1!=r2;break;
case'10':cout<<r1++;break;
case'11':cout<<r2++;break;
case'12':cout<<r1--;break;
case'13':cout<<r2--;break;
default:
break;
}
}
};


void main()//main driver program
{
RatUI r;
r.showmenu();
}
Nov 29 '08 #1
1 1343
boxfish
469 Expert 256MB
If you are getting errors, please post them here so it is easier for people to help you. Also, please use code tags around your code. Put [CODE] before it and [/CODE] after it, so it shows up in a code box. Thanks.
Anyway, your char ch in your RatUI's showmenu function should probably be an int if you're going to be using multiple digit numbers. That will mean changing all the literals in your switch statement to ints. You need a semicolon after your do-while loop in the showmenu function and you need to add the closing brace on that function. That should clear up the first round of errors, but if the compiler complains about there being no input or output operator, you'll need to make the input and output operators friends, instead of members, of the Rational class.
Good luck with this.
Nov 29 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
5
by: John Flynn | last post by:
hi all i'm going to be quick i have an assignment due which i have no idea how to do. i work full time so i dont have the time to learn it and its due date has crept up on me .. As follows:...
0
by: xunling | last post by:
i have a question about answering ..... this topic is "need help" what do i have to write at te topic line, !after i have klicked the "answer message" button ive tried many possibilities,...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.