Use of Template in C++, needs help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2006
Posts: 1
Reputation: abujunad is an unknown quantity at this point 
Solved Threads: 0
abujunad abujunad is offline Offline
Newbie Poster

Use of Template in C++, needs help

 
0
  #1
Jul 26th, 2006
I am new in C++, and learning the langauge by my self only, while practicing i come across one question for which i have no idea, the question is to calculate area and perimeter of rectangle, the code i wrote was

  1. #include <iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5. // declaration of variables
  6. float length; //length of a rectangle
  7. float width; //width of a rectangle
  8. float area; //area of a rectangle
  9. float perim; //perimeter of a rectangle
  10. // prompt for and read the input values
  11. cout << "Enter the length of the rectangle" << endl;
  12. cin >> length;
  13. cout << "Enter the width of the rectangle" << endl;
  14. cin >> width;
  15. //Calculate the area and the perimeter
  16. area = length*width;
  17. perim = 2.0*length + 2.0*width;
  18. //Display the result
  19. cout << "The area is " << area << endl;
  20. cout << "The perimeter is " << perim << endl;
  21.  
  22. return 0;
  23.  
  24. }
BUT I WHAT I NEED IS

The program will read the values of length and width from the user trough keyboard and passes these values to the function calculate(T x, T y). The function calculates the area and perimeter of the rectangle and displays the result on the screen. The function returns no value.

Can someone HELP?

thankx
Last edited by WolfPack; Jul 26th, 2006 at 2:56 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: Use of Template in C++, needs help

 
0
  #2
Jul 26th, 2006
Do you know how to write a function? Surely your book/tutorial/etc tells you how. If you can write a non-templated function (accepting integers as arguments, perhaps) it shouldn't be hard to make it templated.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,623
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: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Use of Template in C++, needs help

 
0
  #3
Jul 26th, 2006
Maybe this can give a rough idea on how a function is organized

  1. float calculateArea (float length, float breadth)
  2. {
  3. return (length * breadth);
  4. }
  5.  
  6. float calculatePerimeter (float length, float breadth)
  7. {
  8. return (2 * length + 2 * breadth);
  9. }

and this can be called in main using

  1. cout << "THe area of reactangle is " << calcuateArea (length, width);
  2. cout << "THe perimeter of reactangle is " << calcuatePerimeter (length, width);

Hope it helped,
Bye.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC