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

#include <iostream> 
using namespace std; 
int main () 
{ 
       // declaration of variables 
        float length; //length of a rectangle 
        float width; //width of a rectangle 
        float area; //area of a rectangle 
        float perim; //perimeter of a rectangle 
        // prompt for and read the input values 
        cout << "Enter the length of the rectangle" << endl; 
        cin >> length; 
        cout << "Enter the width of the rectangle" << endl; 
        cin >> width; 
        //Calculate the area and the perimeter 
        area = length*width; 
        perim = 2.0*length + 2.0*width; 
        //Display the result 
        cout << "The area is " << area << endl; 
        cout << "The perimeter is " << perim << endl; 
         
        return 0; 
 
}

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

Dani AI

Generated

A simple way to meet the requirement "calculate(T x, T y)" is to move the rectangle math into a function template that prints the results and returns nothing. This follows 's point that templated functions are the same pattern as ordinary functions, and it consolidates the separate area/perimeter routines suggested by into one reusable routine that works for float, double, int, etc.

#include <iostream>

template<typename T>
void calculate(T length, T width)
{
    T area = length * width;
    T perimeter = T(2) * (length + width);
    std::cout << "Area: " << area << '\n'
              << "Perimeter: " << perimeter << '\n';
}

int main()
{
    double len = 0.0, wid = 0.0;
    if (!(std::cin >> len >> wid)) return 1;  // basic input check
    calculate(len, wid);
    return 0;
}

Notes and quick troubleshooting:

  • If the call uses integer types the results will be integer arithmetic; use float/double or cast to get fractional results.
  • To accept two different numeric types (e.g., int and double) use a two-parameter template and compute a common result type with std::common_type (C++11+).
  • Prefer std:: prefixes instead of using namespace std; and always validate std::cin input.
  • For more details on function templates see function templates (cppreference).

Recommended Answers

All 2 Replies

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.

Maybe this can give a rough idea on how a function is organized

float calculateArea (float length, float breadth)
{
    return (length * breadth);
}

float calculatePerimeter (float length, float breadth)
{
   return (2 * length + 2 * breadth);
}

and this can be called in main using

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

Hope it helped,
Bye.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.