Getting wrong answers from program.

So my assignment is to write a complete program that prompts the user to input the radius of a circle(in main function),and calculates the diameter,circumference and area respectively(in three user-deļ¬ned functions),then prints those values to the screen(in main function).
I wrote this program and it runs but gives the wrong answers. For example, I enter a radius of 5 and it tells me the diameter is 00EF11CC. Why isn't it printing the right values?

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

double diam(double x, double y)
{
	double d, r;
	d = 2 * r;
	return d;
}

double circ(double x, double y)
{
	double c, r;
	c = 2 * 3.1416 * r;
	return c;
}

double area(double x, double y)
{
	double a, r;
	a = 3.1416 * r * r;
	return a;
}

int main()
{
	double r;
	
	cout << "Enter the radius of the circle: ";
	cin >> r;

	cout << "The diameter is " << diam << endl;
	cout << "The circumference is " << circ << endl;
	cout << "The area is " << area << endl;

	system("pause");
	return 0;

}
Last edited on
Hint from my compiler:

(line 14): warning C4700: uninitialized local variable 'r' used
(line 21): warning C4700: uninitialized local variable 'r' used
(line 7): warning C4700: uninitialized local variable 'r' used

Okay so I changed the parameters from
1
2
3
double diam(double x, double y)
double circ(double x, double y)
double area(double x, double y)

to
1
2
3
double diam(double r)
double circ(double r)
double area(double r)

but I'm still getting
1
2
3
32:32: warning: the address of 'double diam(double)' will always evaluate as 'true' [-Waddress]
33:37: warning: the address of 'double circ(double)' will always evaluate as 'true' [-Waddress]
34:28: warning: the address of 'double area(double)' will always evaluate as 'true' [-Waddress]
Here's the issue.For example your first function:
1
2
3
4
5
6
double diam(double x, double y)
{
	double d, r;
	d = 2 * r;
	return d;
}


you pass in x and y, but never use them. Even worse than that, you use r in your diameter calculation, but this is undefined.

If you changed it to this:
1
2
3
4
5
6
double diam(double r)
{
	double d;
	d = 2 * r;
	return d;
}

it will work.
but I'm still getting


that has nothing to do with your code in your methods, that's actually to do with your code in main:
This is simply the wrong way to call a function:
cout << "The diameter is " << diam << endl;

it needs to be something like this:
cout << "The diameter is " << diam(r) << endl;

After all, you've as the user for the radius so why don't you actually use it? :)

I suggest reading this:
http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
Oooh okay I get it now. Thank you very much for your help and the link. I'll be sure to read more on this topic.
no worries dude, happy coding.
Topic archived. No new replies allowed.