What's Wrong With My Functions?

This code should be pretty straightforward, readData reads the width and length, area calculates the area and stores it in double Rav, and then printOutput prints the output. However none of it works when I run the program. I think I'm calling the functions to the main block of code incorrectly. I really need help understanding this. This particular question a practice problem for a quiz tomorrow so I need to understand what I'm doing wrong.

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
#include <iostream>
void readData(double w, double l);
double area(double w, double l);
void printOutput(double w, double l, double Rav);
using namespace std;
int main()
{
	void readData(double w, double l);
	double area(double w, double l);
	void printOutput(double t, double z, double Area);
}
void readData(double w, double l)
{
	cout << "Enter the width and length of the rectangle" << endl;
	cin >> w >> l;
}
double area(double w, double l)
{
	double Rav;
	Rav = w * l;
	return Rav;
}
void printOutput (double w, double l, double Rav)
{
	cout << "The length of the rectangle is " << l << endl;
	cout << "The width of the rectangle is " << w << endl;
	cout << "The area of the rectangle is " << Rav << endl;
}

Last edited on
Scroll down to "arguments by reference" in http://www.cplusplus.com/doc/tutorial/functions/

Edit: Read the "how to call a function" part earlier too.
Last edited on
You have a couple of other problems also:

1
2
3
4
5
6
int main()
{
  void readData(double w, double l);
  double area(double w, double l);
  void printOutput(double t, double z, double Area);
}

main has no executable logic. Those statements are function prototypes, not function calls.

main should also return 0;

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Also - in main() all the statements are trying to redefine your original function protocols. Leave out

1
2
3
void
double
void


when calling your functions.

You only need to declare variables l and w once, and you should do it outside of the function calls.

You return a value for area Rav from function double area(double w, double l) to main(), but main() doesn't store that value anywhere. If you save it to a variable in main(), you can use it as your third parameter double Area passed to void printOutput. . .

Variables double t, double z are unnecessary since they represent width and length, for which you have already defined double w, double l
I made some slight modifications but the readData is still giving me trouble. If I ask for the values before I get to readData, the program runs but it asks for the values twice. If I take out the lines before readData, it won't run because variables l and w are uninitialized. *Also I can't just take out the readData function because my professor wants us to do a function similar to that on the quiz.*
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
#include <iostream>
void readData(double w, double l);
double area(double w, double l);
void printOutput(double w, double l, double Rav);
using namespace std;
int main()
{
	double w, l, a;
	cout << "Please enter the width and length of a rectangle" << endl;
	cin >> w >> l;
	readData(w,l); //If I take out the above lines of code it tells me that w and l are uninitialized values
	a = area(w, l);//If I leave in the above lines, it asks for the values twice
	printOutput(w, l, a);
	return 0;
}
void readData(double w, double l)
{
	cout << "Enter the width and length of the rectangle" << endl;
	cin >> w >> l;
}
double area(double w, double l)
{
	double Rav;
	Rav = w * l;
	return Rav;
}
void printOutput (double w, double l, double Rav)
{
	cout << "The length of the rectangle is " << l << endl;
	cout << "The width of the rectangle is " << w << endl;
	cout << "The area of the rectangle is " << Rav << endl;
}
Last edited on
The readData has to use by reference parameters.


Write line 8 as:
1
2
3
double w = 0.0;
double l = 0.0;
double a = 0.0;

Then you don't have uninitialized variables.
Of course I have to set them to 0! I always forget the simplest of things on these. Thank you all very much, I'll have to do some more practice problems tonight.
Edit: Setting them to 0 makes sense but now it's just saying everything equals 0.
Last edited on
Walk through your code:

You are asking for l and w in lines 9 - 10, and again by calling the function readData in line 11, right?

keskiverto is right - uninitialized variables can cause a lot of headaches, best to assign some value upon declaration so you don't make false assumptions later about what they represent. In this case, since you are assigning values in functions readData and area, the absence of initialization wouldn't cause a crash, but it's still a good idea to make that initialization a habit.

to make readData use reference parameters you only need to insert one character in four places: twice in your prototype and twice in the actual parameters of the function. Add that single referencing character to change the parameters to addresses of variables l and w. . .

So - to summarize: Remove lines 9 and 10, initialize w, l and a, then change the parameters for readData to references, and you've got it made!
One more very little thing, and it's a preference rather than anything else:

in a short program like this one, with fairly clear meanings and uses, the cryptic use of "l", "w", "a," etc. isn't a problem.

However, if you intend to write the next best-selling 2-million code line game, probably a good idea to be more descriptive. . .length, width and area come to mind as likely names, plus maybe calculateArea or something similar to that so you can keep track of what the function actually does by its name. You know now what it's for, but in a more complex program, not so much several months down the road.
Last edited on
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
#include <iostream>
void readData(double &w, double &l);
double area(double w, double l);
void printOutput(double w, double l, double Rav);
using namespace std;
int main()
{
	double w = 0;
	double l = 0;
	double Rav = 0;
	readData(w, l);
	Rav = area(w, l);
	printOutput(w, l, Rav);
	return 0;
}
void readData(double &w, double &l)
{
	cout << "Enter the width and length of the rectangle" << endl;
	cin >> w >> l;
}
double area(double w, double l)
{
	double Rav;
	Rav = w * l;
	return Rav;
}
void printOutput (double w, double l, double Rav)
{
	cout << "The width of the rectangle is " << w << endl;
	cout << "The length of the rectangle is " << l << endl;
	cout << "The area of the rectangle is " << Rav << endl;
}


Okay everything seems to be in working order now. Thanks a ton for the help, I think I'm actually starting to get the hang of functions now.
Topic archived. No new replies allowed.