I am trying to figure out why my function isn't working when I compile the program....can anyone suggest anything?

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

double distance();

void main()
{
	double center1, center2, point1, point2;

	cout << "Please Enter X-Value of Center: ";
	cin >> center1;
	cout << "Please Enter Y-Value of Center: ";
	cin >> center2;
	cout << endl;

	cout << "Please Enter X-Value of Point: ";
	cin >> point1;
	cout << "Please Enter Y-Value of Point: ";
	cin >> point2;
	cout << endl;

	cout << "Distance between points: " << distance();
	cout << endl;

} //End of Main

double distance()
{
	double center1, center2, point1, point2;
	double distance1, distance2, distance3;

	if (point1 >= center1 && point2 >= center2 )
		distance1 = (point1 - center1); 
		distance2 = (point2 - center2);
		distance3 = pow((distance1 + distance2), (1/2));
		cout << "Distance Between Points: " << distance3;
	else
		cout << "Points Will Produce an Unreal Number"; 
		cout <<endl;

	return distance3;
}

Post some of the compiler error messages.


>>void main()

NEVER EVER use void main() Here's why

1>------ Build started: Project: Assignment6, Configuration: Debug Win32 ------
1>Compiling...
1>Assignment6.cpp
1>n:\baker school\trcc intro to programming\assignment#8\assignment6\assignment6.cpp(42) : error C2181: illegal else without matching if
1>Build log was saved at "file://n:\Baker School\TRCC Intro to Programming\Assignment#8\Assignment6\Debug\BuildLog.htm"
1>Assignment6 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Look at line 42 as stated in the error message (line 32 of your post). Multi-line if statements have to be enclosed in { and } braces

if( something )
{
   // blabla
}
else
{
   // blabla
}

What should I use instead on void main() ? I'm new to C++, so I haven't used too many things.....

These are the error messages that I'm getting now, once I use the brackets:

1>------ Build started: Project: Assignment6, Configuration: Debug Win32 ------
1>Compiling...
1>Assignment6.cpp
1>n:\baker school\trcc intro to programming\assignment#8\assignment6\assignment6.cpp(37) : warning C4700: uninitialized local variable 'point1' used
1>n:\baker school\trcc intro to programming\assignment#8\assignment6\assignment6.cpp(37) : warning C4700: uninitialized local variable 'center1' used
1>n:\baker school\trcc intro to programming\assignment#8\assignment6\assignment6.cpp(37) : warning C4700: uninitialized local variable 'point2' used
1>n:\baker school\trcc intro to programming\assignment#8\assignment6\assignment6.cpp(37) : warning C4700: uninitialized local variable 'center2' used
1>Linking...
1>Embedding manifest...
1>Build log was saved at "file://n:\Baker School\TRCC Intro to Programming\Assignment#8\Assignment6\Debug\BuildLog.htm"
1>Assignment6 - 0 error(s), 4 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

you need to pass the variables point1, point2, center1 and center2 from the main() function to the distance() function, so that the values obtained in the main function can be passed to the distance() function
The prototype of the distance function can be

double distance(double point1, double point2, double center1 ,double center2 );

.

Like super-sonic said, you need to pass your variables.

This code :

double distance()

{

double center1, center2, point1, point2;

double distance1, distance2, distance3;

 

if (point1 >= center1 && point2 >= center2 )

distance1 = (point1 - center1);

distance2 = (point2 - center2);

distance3 = pow((distance1 + distance2), (1/2));

cout << "Distance Between Points: " << distance3;

else

cout << "Points Will Produce an Unreal Number";

cout <<endl;

 

return distance3;

}

You DECLARE point1, point 2, center 1, center2 inside your function, so that
variable will be used, instead of the one you use in main. What you should
do is make your function take in parameters like super-sonic showed, then you can call it in main like so :

cout << "Distance between points: " << distance(point1,point2mcenter1,center2);

>>What should I use instead on void main() int main()

Ok, so I've made the changes, but I'm still getting the following error:

1>------ Build started: Project: Assignment6, Configuration: Debug Win32 ------
1>Compiling...
1>Assignment6.cpp
1>Linking...
1>Assignment6.obj : error LNK2019: unresolved external symbol "double __cdecl distance(void)" (?distance@@YANXZ) referenced in function _main
1>E:\Baker School\TRCC Intro to Programming\Assignment#8\Assignment6\Debug\Assignment6.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://e:\Baker School\TRCC Intro to Programming\Assignment#8\Assignment6\Debug\BuildLog.htm"
1>Assignment6 - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

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

double distance();
double radius();

int main()
{
	double center1, center2, point1, point2;

	cout << "Please Enter X-Value of Center: ";
	cin >> center1;
	cout << "Please Enter Y-Value of Center: ";
	cin >> center2;
	cout << endl;

	cout << "Please Enter X-Value of Point: ";
	cin >> point1;
	cout << "Please Enter Y-Value of Point: ";
	cin >> point2;
	cout << endl;

	cout << "Distance between Points: " << distance();
	cout << endl;

} //End of Main

double distance(double center1, double center2, double point1, double point2)
{
	double distance1, distance2, distance3;

	if (point1 >= center1 && point2 >= center2 )
	{
		distance1 = pow((point2 - center1), 2); 
		distance2 = pow((point2 - center1), 2);
		distance3 = pow((distance1 + distance2), 0.5);
		cout << "Distance Between Points: " << distance(center1,center2,point1,point2);
	}
	else
	{
		cout << "Points Will Produce an Unreal Number"; 
	}

	return distance3;
}

Ok.....I've got it to work so far, but now I need to add a function to find the radius ( r=d/2 )....how do I add the distance that I just found and call it through my radius function?

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

double distance(double center1, double center2, double point1, double point2);
double radius(double distance3);

int main()
{
	double center1, center2, point1, point2, distance3;

	cout << "Please Enter X-Value of Center: ";
	cin >> center1;
	cout << "Please Enter Y-Value of Center: ";
	cin >> center2;
	cout << endl;

	cout << "Please Enter X-Value of Point: ";
	cin >> point1;
	cout << "Please Enter Y-Value of Point: ";
	cin >> point2;
	cout << endl;

	cout << "Distance between Points: " << distance(center1,center2,point1,point2);
	cout << endl;
	cout << "Radius of Points is: " << radius(distance3);
	cout << endl;

} //End of Main

double distance(double center1, double center2, double point1, double point2)
{
	double distance1, distance2, distance3;

	distance1 = pow((point1 - center1), 2); 
	distance2 = pow((point2 - center2), 2);	
	distance3 = pow((distance1 + distance2), 0.5);

	return distance3;
}

double radius(double distance3)
{
	double rad;

	rad = distance3 / 2;

	return rad;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.