Calculating an area for the given width and length C++ code sample demonstrating user defined function

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Calculating an area for the given width and length C++ code sample demonstrating user defined function

To show: How to calculate an area for the given width and length C++  program example to show the user defined function

 

// demonstrates the use of function prototypes

#include <iostream>

 

// another method simplifying type identifier using typedef

// the words unsigned short is simplified to USHORT

typedef unsigned short USHORT;

 

// function prototype

USHORT FindTheArea(USHORT length, USHORT width);

 

int main(void)

{

USHORT lengthOfYard;

USHORT widthOfYard;

USHORT areaOfYard;

std::cout<<"\nThe wide of your yard (meter)? ";

std::cin>>widthOfYard;

std::cout<<"\nThe long of your yard( meter)? ";

std::cin>>lengthOfYard;

 

// function call

areaOfYard = FindTheArea(lengthOfYard, widthOfYard);

 

std::cout<<"\nYour yard is ";

std::cout<<areaOfYard;

std::cout<<" square meter\n\n";

 

return 0;

}

 

// function definition

USHORT FindTheArea(USHORT l, USHORT w)

{

return (l * w);

}

 

Output example:

 

The wide of your yard (meter)? 10

The long of your yard( meter)? 21

Your yard is 210 square meter

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index