“It seems I can’t get this program in C ++ to work, I tried for several hours, and I don’t understand this concept. Here is a question from the book:
"Write a C ++ function:
void rect (int& ar, int& per, int len, int wid)
which calculates the area ar and the perimeter in a rectangle with len length and width width. Test it with the main program, which enters the length and width of the rectangle and displays its area and perimeter. Print the value in the main program, not in the procedure.
Here is what is written in the instructions of the instructors:
** # 13 from text, p. 83: for this program you need to use a function that takes the length and width of your rectangle from main(), calculates the area and perimeter, and returns these values in arand per. Yours then main()displays these values. Variables arand permust be declared in main(), and then passed by reference to the method rect().
Here is the code that I have written so far. I got stuck on passing a variable with a reference to the rect () method:
#include <iostream>
using namespace std;
void rect(int& ar, int& per, int len, int wid)
{
ar = len * wid;
cout << "Area: " << ar << ".";
}
int main ()
{
int ar, per, len, wid;
cout << "Please enter the length of the rectangle: ";
cin >> len;
cout << "Please enter the width of the rectangle: ";
cin >> wid;
return 0;
}
I know that the calculation is correct in the rect () method, because it compiles correctly. I cannot figure out how to call the rect () method from main. Every time I tried something, it gives me "a few arguments for the function'void rect(int&, int&, int, int)' error.
I am using JGrasp for OS X as my IDE.
Please, help! "