Using Functions to find Circumference, Distance, radius and area

I'm having trouble with the output of this function. i dont know whether i need to have parameters for the radius, circumference, and area functions or not.When I output the functions it was 0 for all of them.

#include <fstream>
#include <cmath>
using namespace std;

ifstream infile;
ofstream outfile;

double distance(int x1,int y1,int x2, int y2);
double radius();
double circumference();
double area();

int main()
{
int pointa, pointb, pointc, pointd;

infile.open("inCircle.txt");
outfile.open("outCircle.txt");

infile>>pointa>>pointb>>pointc>>pointd;

outfile<<"The radius is "<<radius()<<endl<<endl;
outfile<<"The circumference is "<<circumference()<<endl<<endl;
outfile<<"The area is "<<area()<<endl<<endl;

infile.close();
outfile.close();

system("pause");
return 0;
}

double distance(int x1,int y1,int x2, int y2)
{
double distance;

distance = sqrt((x2-x1)*(x2-x2)+(y2-y1)*(y2-y1));

return distance;
}

double radius()
{
double radius;
int p1=0, p2=0, p3=0, p4=0;

radius = distance (p1,p2,p3,p4);

return radius;
}

double circumference()
{
double circumference, r;

r = radius();

circumference = 2*3.1416*r;

return circumference;
}

double area()
{
double area, r;

r = radius();

area = 3.1416*r*r;

return area;
}
closed account (48T7M4Gy)
A couple of suggestions to start:
1. Please put code tags top and bottom of your listing.
2. Instead of reading from a file try setting some hard-coded values to make debugging easier.
3. The reason you are getting zero return values is because you aren't sending a parameter list from main via the functions to the function itsel. The functions are good but they are returning local variable reuslts which are zero.
4. For circumference say, you need to write your function something like
1
2
3
4
double circumference( double radius)
{
    return 2 *pi* radius;
}

closed account (48T7M4Gy)
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
#include <iostream>
#include <fstream>
#include <cmath>

double distance(int, int, int, int);
double radius(int, int, int, int);
double circumference(int, int, int, int);
double area(int, int, int, int);

int main()
{
	std::ifstream infile; // this should have been in main()
	std::ofstream outfile; // this should have been in main()
	
	int pointa = 5, pointb = -9, pointc = 12, pointd = 15;

	std::cout << distance(pointa, pointb, pointc, pointd) << std::endl;
	std::cout << radius(pointa, pointb, pointc, pointd) << std::endl;

	/// blah blah blah

	return 0;
}

double distance(int x1, int y1, int x2, int y2)
{
	double distance;
	distance = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1)); // fixed error
	return distance;
}

double radius(int x1, int y1, int x2, int y2)
{
	double radius;
	radius = distance(x1, y1, x2, y2);
	return radius;
}

double circumference(int x1, int y1, int x2, int y2)
{
	double circumference, r;
	r = radius(x1, y1, x2, y2);
	circumference = 2 * 3.1416*r;
	return circumference;
}

double area(int x1, int y1, int x2, int y2)
{
	double area, r;
	r = radius(x1, y1, x2, y2);
	area = 3.1416*r*r;
	return area;
}
Topic archived. No new replies allowed.