Help with functions

This is my first time using functions. Im suppose use functions ans make a area of a rectangle calc. For som reason the getArea function is not wotking, any help? I accidentaly ended up typing triangle but its suppose to rectangle.
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>

using namespace std;
void getWidth(int);
void getLength(int);
void getArea( int, int, float);

int main()
{
	int width = 0 ;
	int length = 0 ;
	int area = length * width;
	cout << "Please enter the rectangles width " << endl;
		getWidth( width);
		getLength( length);
		getArea(length, width, area);
		int area = length * width;

	system("PAUSE");
	return 0;

}

void getWidth(int width)
{
	cin >> width;
	cout <<"The width is " << width << endl;

}

void getLength(int length)
{
	cout << " Pease enter the triangles length." << endl;
	cin >> length;

	cout << "The triangles length is "<< length << endl;

}

void getArea(int length, int width, float area)
{
	area = length * width;
	cout << "The area of the triangle is " << area;

}
Last edited on
My advice to you would be to read and focus mainly on learning to pass variables through functions properly. I think the reason it wasn't working out for you is because you weren't passing through reference



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>

using namespace std;
void getWidth(int&);
void getLength(int&);
void getArea( int&, int&, int&);

int main()
{
	int width = 0 ;
	int length = 0 ;
	int area; //You had called the Variable "area" an Int when you declared it a float at the prototype function
	// It was also wrong when you tried converting two int variables into float variables.
		getWidth( width);
		getLength( length);
		getArea(length, width, area);
		// unnecessary to write int area = length * width. twice inside int main() when you declared it inside the function

	system("PAUSE");
	return 0;

}

void getWidth(int& width)
{
	cout << "Please enter the rectangles width: "; //instead of writing the cout statement inside int main(). You can write it here.
	cin >> width;
	cout <<"The width is: " << width << endl;

}

void getLength(int& length)
{
	cout << "Pease enter the triangles length: ";
	cin >> length;
	cout << "The triangles length is: " << length << endl;

}

void getArea(int& length, int& width, int& area)
{
	area = length * width;
	cout << "The area of the triangle is: " << area << endl;

}
Last edited on
Thank you very much, i have started practicing functions with referneces now, really appreciate the comments. Helped a ton.
Topic archived. No new replies allowed.