Calculate Area

I have to write a program that reads length and width from user, calculates and displays area. Also, I need to make separate functions for length, width, area, and display. I do not know how to calculate area.


// This is my code

#include <iostream>
using namespace std;

void getLength();
void getWidth();
void calculateArea();


void main()
{
getLength();
getWidth();
calculateArea();
} // Main


void getLength()
{
int length;
cout << "Enter length: " << endl;
cin >> length;
} // getLength


void getWidth()
{
int width;
cout << "Enter width: " << endl;
cin >> width;
} // getWidth


void calculateArea()
{
int area;

// How to calculate area using data from getLength and getWidth?

} // calculateArea
What crap. Not you but silly thing to separate those commands into 3 separate function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

int length;
int width;
int height;

void SetWidth()
{
	int temp;
	std::cout << "Please enter the width: ";
	if(std::cin >> temp)
	{
		width = temp;
	}
	else
	{
		std::cout << "Error while inputting width.\n";
	}
}

void SetLength()
{
	int temp;
	std::cout << "Please enter the length: ";
	if(std::cin >> temp)
	{
		length = temp;
	}
	else
	{
		std::cout << "Error while inputting length.\n";
	}
}

void GetArea()
{
	std::cout << "The area is " << length*width << "\n";
}

int main()
{
	GetWidth();
	GetLength();
	GetArea();
}


EDIT:

Could of easily been a single function. :/

 
int Area(int l, int w) { return l*w; }


EDIT 2:

The problem is that for this to work you had to declare the variables globally, when you declare them in a function, they are local to the function and nothing but the function can access it ( Unless special things you don't need to worry about just yet )

Just out of curiosity, is this college/university work?
Last edited on
No globals, please. This homework does clearly involve function parameters and returning a value from a function.

I do not know how to calculate area.

Seriously? http://en.wikipedia.org/wiki/Area#Rectangles
keskiverto I'm with you on no globals.

This being probably an assignment and the excessive use of void functions I figured this is how the professor wanted it done ( I have seen a lot of stupid homework questions getting students to do things in such an awful way)
Hi,
if you don't want (in fact, shouldn't) use global var.s, so, why you're using void functions for getWidht() and getLength() functions?!
use double or int type for getLength() and getLength() functions, then in your main functions, define two var.s, like width and length, then assign return value of those functions to them(width = getWidth() and so on... ), and call getArea() function with that parameters.(getArea(width, lengh))

***NOTE: its better to define your functions with lowercase letters(for first letter), and define your classes names with uppercase letters, but its optional.
@ megatron 0, @ keskiverto, @ amirtork, Thank you guys for your help. Now my homework is perfect.
Topic archived. No new replies allowed.