Function help with Program Perimeter

Hello, I am currently trying to make a program without changing this program in which I will put. I can only add functions and comments to the code. My confusion is that I have no idea how to finish it, although I understand a bit about functions (I was absent from my last 2 classes due to issues), I am in confusion on how to add it in this set of code. I'm looking for tips and help with the program, and I greatly appreciate any help. I'm trying to run my program like this:

Enter rectangle length: -1 <-- invalid value, ask user to re-enter
Enter rectangle length: -2 <-- Invalid value
Enter rectangle length: 0 <-- Invalid value
Enter rectangle width: 3 <-- Valid, accept
Enter rectangle width: -5 <-- invalid Value
Enter rectangle width: 0 <-- invalid value
Enter rectangle width: 5 <-- Valid, accept

Rectangle Data:
---------------
Length: 3
Width: 5
Area: 15
Perimeter: 16
 


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
  #include <iostream>
using namespace std;

void getLengthAndWidth(double&, double&);
double calcArea(double, double);
double calcPerimeter(double, double);
void displayData(double, double, double, double);

int main()
{
    double length, width, area, perimeter;
    
    // get the length and the width from the user
    getLengthAndWidth(length, width);
    
    // calculate the area of the rectangle
    area = calcArea(length, width);
    
    // calculate the perimeter of the rectangle
    perimeter = calcPerimeter(length, width);
    
    // display above data to the user
    displayData(length, width, area, perimeter);
    
    return 0;
}
I think you can easily do calcArea, calcPerimeter and displayData as it is easy. Here is first function :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void getLengthAndWidth(double& l, double& w){
    cout << "Enter length";
    cin >> l;
    while(l <= 0){
        cout << "Invalid value, reenter : ";
        cin >> l;
    } 
    cout << "Enter width";
    cin >> w;
    while(w <= 0){
    cout << "Invalid value, reenter : ";
    cin >> w;
    }
}

If you have any other doubt, please ask.
Last edited on
Thanks for the response, I had to change some of the things in with the function. I tried fixing the other ones, such as calcArea, calcPerimeter, and displayData, but it never seemed to work. It kept giving me errors. I don't know if I am supposed to do the same scenario you did with the functions to the rest of them, or just the void?

I appreciate the help however, it's starting to make sense as what you did with the first one.

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
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
using namespace std;

void getLengthAndWidth(double&, double&);
double calcArea(double, double);
double calcPerimeter(double, double);
void displayData(double, double, double, double);

int main()
{
	double length, width, area, perimeter;

	// get the length and the width from the user
	void getLengthAndWidth(double& length, double& width) {
		cout << "Enter your length please";
		cin >> length;
		while (1 <= 0) {
			cout << "Invalid value, please re-enter: ";
			cin >> length;
		}
		cout << "Please Enter your Width";
		cin >> width;
		while (width <= 0) {
			cout << "Invalid value, reenter: ";
			cin >> width;
		}
	}
	
	// calculate the area of the rectangle
	area = calcArea(double, double) {
		area = length * width;
		return 0;
	}

	// calculate the perimeter of the rectangle
	perimeter = calcPerimeter(length, width) {
		perimeter = length + width;
		return 0;
	}



	// display above data to the user
	void displayData(length, width, area, perimeter) {
		cout << "Rectangle Data: " << endl;
		cout << "---------------" << endl;
		cout << "Length:" << Length << endl;
		cout << "Width:" << Width << endl;
		cout << "Area:" << area << endl;
		cout << "Perimeter:" << perimeter << endl;
		
		return 0;
	}

	return 0;
}
[
Try the code I posted in this comment.
1. Think of functions as something you give a input, and it returns a output.
2. All the function definitions should be outside main()
3. In main, you "call" the functions by giving it some input, and returns the output. For example, in line 13 you call the function calcperimeter, and give it the parameters length and width. Then it does a process(defined at below main) and returns a value. That value is stored in perimeter.
4. Same with area.
5. void functions don't return a value. They just do the process part, such as taking in a value, or outputting them. So don't put return 0 in void.
6. Read the code line by line, see that the functions don't return 0, but the calculated value.
7. Hope I cleared your doubts, if you have any questions please ask. I would like to ask you to revise all the concepts about functions from your notebook or form a website.
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
46
47
48
49
50
#include <iostream>
using namespace std;

void getLengthAndWidth(double&, double&);
double calcArea(double, double);
double calcPerimeter(double, double);
void displayData(double, double, double, double);

int main()
{
	double length, width, area, perimeter;
	getLengthAndWidth(length, width); // takes the length and width
	perimeter = calcPerimeter(length,width); // calculates 2(l + w) and puts it into perimeter
	area = calcArea(length,width); // calculates l * w and puts into area
	displayData(length, width, area, perimeter); // displays the data

	return 0;
}

void getLengthAndWidth(double& length, double& width) {
	cout << "Enter your length please";
	cin >> length;
	while (length <= 0) {
		cout << "Invalid value, please re-enter: ";
		cin >> length;
	}
	cout << "Please Enter your Width";
	cin >> width;
	while (width <= 0) {
		cout << "Invalid value, reenter: ";
		cin >> width;
	}
}

double calcPerimeter(double length, double width) {
	return 2 * (length + width);
}

double calcArea(double length, double width) {
	return length * width;
}

void displayData(double length,double width,double area,double perimeter) {
	cout << "Rectangle Data: " << endl;
	cout << "---------------" << endl;
	cout << "Length:" << length << endl;
	cout << "Width:" << width << endl;
	cout << "Area:" << area << endl;
	cout << "Perimeter:" << perimeter << endl;
}
Last edited on
I appreciate the response, I get it now. I just have two questions about your program. Is that just the rule, whenever there is a function before the "int main()", we must definite it again in the beginning after the int main() again?

Also, to line 36 and 40, why is it "return 2" and "return"? I don't know if I asked my question right, but in my program I tried to do area = length * width? I assume we're adding Return length * width; width because it's a step-by-step procedure?

Again, I greatly appreciate you for the help. I now got a sense of what functions are. I just need more practice! :)
Also, to line 36 and 40, why is it "return 2" and "return"? I don't know if I asked my question right, but in my program I tried to do area = length * width? I assume we're adding Return length * width; width because it's a step-by-step procedure?

You are returning the output of the function, Of course, you can do this inside the function
1
2
area = length * width;
return area;

But that adds an unnecessary line.
The formula of perimeter is 2*(l+b) :) not l+b
return 2*(length+width)
2 multiplied by the sum of length and width.
Line 40, it calculates length * width (lets say 4 * 3 , which is 12) and returns 12.
I appreciate the response, I get it now. I just have two questions about your program. Is that just the rule, whenever there is a function before the "int main()", we must definite it again in the beginning after the int main() again?

I would like to ask you to read about declarations and definitions.
This is a declaration.
double calcArea(double, double);
This is a definition.
1
2
3
double calcArea(double length, double width) {
	return length * width;
}

The declaration introduces the name into scope. It doesn;t say anything about what the function does, only gives the name, output datatype and parameter datatype.
Definition does the above AND gives the process, that is the contents between {..}
You can use two options
1
2
//all definitions here
int main() {  }

Or you can use this option(used in this program)
1
2
3
//all declarations here
int main() { }
//all definitions here 

Well, not exactly, basically a declaration or definition should be above where it is called from. I would like you to read it from a textbook to understand better.
Remember, when you call a function in main, it searches for the function ABOVE main. So, in option 1, it finds the definition directly and proceeds.
In option 2, it searches the function ABOVE main, finds the declaration, then goes to the definition. This is a very simplified way to understand, so again I recommend a book, such as Chapter 8 of Programming : Principles and Practices using c++ to understand this better.
Last edited on
Please reload the page I edited my comment.
Topic archived. No new replies allowed.