944,098 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1709
  • C++ RSS
Sep 26th, 2006
1

Cannot get PAIR::big to just display greater number

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 18
Solved Threads: 0
Newbie Poster
ToySoldier is offline Offline
20 posts
since Oct 2005
Sep 26th, 2006
0

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

Click to Expand / Collapse  Quote originally posted by ToySoldier ...
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:

C++ Syntax (Toggle Plain Text)
  1. int big = e.big () ;
  2. cout << "\nThe bigger number is " << big ;

Hope it helped, bye.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Sep 26th, 2006
0

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

Thanks... I'll try it out.
Reputation Points: 18
Solved Threads: 0
Newbie Poster
ToySoldier is offline Offline
20 posts
since Oct 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: setprecision
Next Thread in C++ Forum Timeline: Soduku puzzle solver





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC