Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ Help

Status
Not open for further replies.

RollerMan

Technical User
Jan 27, 2002
6
US
I am working with post & pre incrementing. I am having a bit of difficulty getting the numbers to increment correctly where I need them. Could someone tell me what I am doing wrong? Here is what I have.

Main()
{
Fraction P=(5,2);//preincrementing
cout<<&quot;The number is:<<++P<<&quot;after incrementing is&quot;<<P<<endl;

cout<<&quot;The number is:<<--P<<&quot;after incrementing is&quot;<<--P<<endl;

************This section above should be outputed
The number is: 7/2 after incrementing is 7/2
The number is: 5/2 after incrementing is 5/2

***************Post Incrementing********************
Fraction Q=(2,3);
cout << &quot;, Q++ is: &quot; << Q++ << &quot;, now Z is: &quot; << Q << endl;

cout << &quot;\tQ is: &quot; << Q;
cout << &quot;, Q-- is: &quot; << Q-- << &quot;, now Q is: &quot; << Q << endl;

The number is: 2/3 after incrementing is 4/3
The number is: 4/3 after incrementing is 2/3

****************************Here is the code for function***

Fraction Fraction::eek:perator ++ ()
{
numerator += denominator;
return *this;
}

Fraction Fraction::eek:perator --()
{
numerator -= denominator;
return *this;
}

Fraction Fraction::eek:perator ++ (int)
{
Fraction temp=*this;
numerator += denominator;
return temp;
}

Fraction Fraction::eek:perator --(int)
{
Fraction temp=*this;
numerator -= denominator;
return temp;
}


The problem is that the numbers are not coming out right. I have looked at several books could someone give me some insite on what I am or am not doing so I could learn?


 
Here is your code,i have made some little correction in it,now it works fine.But since the &quot;class Fraction&quot; was missing i could'nt tell if it was exactly this that you want it to do.
Perhaps this should help a little bit.

#include <iostream>
using namespace std;

class Fraction
{
public:
int numerator;
int denominator;
int operator ++ ();
int operator ++ ( int );
int operator --();
int operator --( int );
};

Fraction f;

void main()
{
double P = 2.5;//preincrementing

cout<<&quot;The number is:&quot;<<++P<<&quot;after incrementing is&quot;<<P<<endl;

cout<<&quot;The number is:&quot;<<--P<<&quot;after incrementing is&quot;<<--P<<endl;

//************This section above should be outputed
//The number is: 7/2 after incrementing is 7/2
//The number is: 5/2 after incrementing is 5/2

//***************Post Incrementing********************
double Q=0.666;
cout << &quot;, Q++ is: &quot; << ++Q << &quot;, now Z is: &quot; << Q << endl;

cout << &quot;\tQ is: &quot; << Q;
cout << &quot;, Q-- is: &quot; << --Q << &quot;, now Q is: &quot; << Q << endl;

//The number is: 2/3 after incrementing is 4/3
//The number is: 4/3 after incrementing is 2/3
}
//****************************Here is the code for function***

Fraction::eek:perator ++ ()
{
f.numerator += f.denominator;
return f.numerator;
}

Fraction::eek:perator --()
{
f.numerator -= f.denominator;
return f.numerator;
}

Fraction::eek:perator ++ ( int )
{
f.numerator += f.denominator;
return f.numerator;
}

Fraction::eek:perator --( int )
{
f.numerator -= f.denominator;
return f.numerator;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top