Cannot get PAIR::big to just display greater number

Reply

Join Date: Oct 2005
Posts: 20
Reputation: ToySoldier is an unknown quantity at this point 
Solved Threads: 0
ToySoldier's Avatar
ToySoldier ToySoldier is offline Offline
Newbie Poster

Cannot get PAIR::big to just display greater number

 
1
  #1
Sep 26th, 2006
Hi,

Appreciate any help. My code compiles... builds.... executes just fine. Only problem I'm having is... I cant figure out how to get PAIR::big() to simply print out the larger #. When I execute... it displays... the Larger # like I want... but then for some reason it displays the smaller number next to it. I want to get rid of the smaller #.

Any suggestions?

Thanks Mike

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class PAIR
  5.  
  6. {
  7. private:
  8. int a;
  9. int b;
  10.  
  11. public:
  12.  
  13. void print();
  14. PAIR();
  15. PAIR(int);
  16. PAIR(int,int);
  17. ~PAIR();
  18.  
  19. void swap();
  20. int diff();
  21. int big();
  22. int area();
  23. };
  24.  
  25. int main()
  26. {
  27. PAIR c, d(2), e(12,13);
  28.  
  29. int ans;
  30. c.print();
  31. d.print();
  32. e.print();
  33.  
  34. e.swap();
  35. e.print();
  36.  
  37. ans = c.diff();
  38. cout << ans << endl;
  39.  
  40. e.big();
  41. e.print();
  42.  
  43. return 0;
  44. }
  45.  
  46. PAIR::PAIR()
  47. {
  48. a = 2;
  49. b= 3;
  50. }
  51.  
  52. void PAIR::print()
  53. {
  54. cout << a << " " << b << endl;
  55. }
  56.  
  57. PAIR:: ~PAIR()
  58. {
  59. cout << "Display Destructor Message" << endl;
  60. }
  61.  
  62. PAIR::PAIR(int p1)
  63. {
  64. a=p1;
  65. b=p1;
  66. }
  67.  
  68. PAIR::PAIR(int p1,int p2)
  69. {
  70. a=p1;
  71. b=p2;
  72. }
  73.  
  74. void PAIR::swap()
  75. {
  76. int c;
  77. c=a;
  78. a=b;
  79. b=c;
  80. }
  81.  
  82. int PAIR::diff()
  83. {
  84. return b - a;
  85. }
  86.  
  87. int PAIR::big()
  88. {
  89. if (a > b)
  90. {
  91. return a;
  92. }
  93.  
  94. else
  95. {
  96. return b;
  97. }
  98. }
Last edited by ToySoldier; Sep 26th, 2006 at 6:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Cannot get PAIR::big to just display greater number

 
0
  #2
Sep 26th, 2006
Originally Posted by ToySoldier View Post
Hi,
 #include <iostream>
using namespace std;
  class PAIR
 
{
private:
 int a;
 int b;
 
public:
 
 void print();
 PAIR();
 PAIR(int);
 PAIR(int,int);
 ~PAIR();

 void swap();
 int diff();
 int big();
 int area();
};

int main()
{
PAIR c, d(2), e(12,13);

int ans;
c.print();
d.print();
e.print();

e.swap();
e.print();

ans = c.diff();
cout << ans << endl;

e.big();
e.print(); 

return 0;
}

PAIR::PAIR() 
{
a = 2;
b= 3;
}

void PAIR::print()
{
cout << a << " " << b << endl;
}

PAIR:: ~PAIR()
{
cout << "Display Destructor Message" << endl;
}

PAIR::PAIR(int p1)
{
a=p1;
b=p1;
}

PAIR::PAIR(int p1,int p2)
{
a=p1;
b=p2;
}

void PAIR::swap()
{
 int c;
 c=a;
 a=b;
 b=c;
}

int PAIR::diff()
{
return b - a;
}

 int PAIR::big()
{
if (a > b)
{
return a;
}

else 
{
return b;
} 
}
The reason is that there is no temporary variable to accept the bigger value returned by the "big()" function and hence you end up printing the original value of your object "e". So you can write something like this:

  1. int big = e.big () ;
  2. cout << "\nThe bigger number is " << big ;

Hope it helped, bye.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 20
Reputation: ToySoldier is an unknown quantity at this point 
Solved Threads: 0
ToySoldier's Avatar
ToySoldier ToySoldier is offline Offline
Newbie Poster

Re: Cannot get PAIR::big to just display greater number

 
0
  #3
Sep 26th, 2006
Thanks... I'll try it out.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC