#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cctype>

using namespace std;
double distance(double,double,double,double);
int main()

{double X,secX,Y,secY,z,r;
r=0;

cout<< "Please enter the x value of the center of the circle:"<< endl;
cin>>secX;
cout<< "Please enter the y value of the center of the circle:" << endl;
cin>>secY;
cout<< "Please enter the x value of a point on the circle:"<<endl;
cin>>X;
cout<< "please enter the y of said point:"<< endl;
cin>>Y;

cout << "number before being taking square root:"<<pow(X-secX,2)+pow(Y-secY,2)<< endl;

system("PAUSE");
return EXIT_SUCCESS;
}

double distance(double X,double Y,double secX,double secY)
{

cout << fixed << showpoint <<setprecision(2)<< sqrt(pow(X-secX,2)+pow(Y-secY,2));

}

double radius(double r,double X,double Y,double secX,double secY)

{ r = double distance( X, Y, secX, secY);

cout<<"radius is = "<< fixed << showpoint <<setprecision(2)<<r<<endl;

}

I m trying to get one function to use a value from another all the while out putting numbers that show the decimal place instead of an exponent value.

What am I doing wrong? I can get one function to work but as soon as I try to format the second it doesnt work anymore...

## All 17 Replies

Well I got it, I must have skipped over the part in the chapter about the return command. That really turned this into a simple problem.

Thanks for the space.

Here is what my code looks like now:

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cctype>

using namespace std;
double distance(double,double,double,double);
int main()

{double X,secX,Y,secY,z,R;
R=0;

cout<< "Please enter the x value of the center of the circle:"<< endl;
cin>>secX;
cout<< "Please enter the y value of the center of the circle:" << endl;
cin>>secY;
cout<< "Please enter the x value of a point on the circle:"<<endl;
cin>>X;
cout<< "please enter the y of said point:"<< endl;
cin>>Y;

cout << "number before being taking square root:"<<pow(X-secX,2)+pow(Y-secY,2)<< endl;

cout <<"distance is = " << distance( X, Y, secX, secY)<< endl;

cout << "Radius is = " << radius( X, Y, secX, secY, R)<< endl;

system("PAUSE");
return EXIT_SUCCESS;
}

double distance(double X,double Y,double secX,double secY)
{

return sqrt(pow(X-secX,2)+pow(Y-secY,2));

}
double radius(double X,double Y,double secX,double secY,double R)
{
R=sqrt(pow(X-secX,2)+pow(Y-secY,2));
return R;

}

Well now I am havin an issue with with getting the "y" that the user inputs to match a boolean expression in my if...else loop.

It should repeat the entire program when a user types in y but it does not.

Can someone provide some insight on this?

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cctype>

using namespace std;
double area(double, double,double,double,double, double,double);
double circ(double, double,double,double,double, double);
double distance(double,double,double,double);
int main()

{double X,secX,Y,secY,z,R, cir,rea;
string c,y,n;

R=0;

cout<< "Please enter the x value of the center of the circle:"<< endl;
cin>>secX;
cout<< "Please enter the y value of the center of the circle:" << endl;
cin>>secY;
cout<< "Please enter the x value of a point on the circle:"<<endl;
cin>>X;
cout<< "please enter the y of said point:"<< endl;
cin>>Y;

cout << fixed << showpoint <<setprecision(2);

cout << "number before being taking square root:"<<pow(X-secX,2)+pow(Y-secY,2)<< endl;

cout <<"distance is = " << distance( X, Y, secX, secY)<< endl;

cout << "Radius is = " << radius( X, Y, secX, secY, R)<< endl;

cout<< "Circumference = "<<  circ(cir, X, Y, secX, secY, R) << endl;

cout << "Area is = " << area( cir, X, Y, secX, secY, R, rea)<< endl;

cout <<"Continue? Enter y or n"<<endl;
cin >> c;

tolower('c');

if (c == y)
main();

else
cout << "Thanks for using the circle calculator!"<<endl;

cout <<c<< endl;

system("PAUSE");
return EXIT_SUCCESS;
}

double distance(double X,double Y,double secX,double secY)
{

return sqrt(pow(X-secX,2)+pow(Y-secY,2));

}
double radius(double X,double Y,double secX,double secY,double R)
{
R=sqrt(pow(X-secX,2)+pow(Y-secY,2));
return R;

}
double circ(double cir, double X,double Y,double secX,double secY, double R)
{
R=radius( X, Y, secX, secY, R);
cir = R*2*3.1416;

return cir;
}
double area(double cir, double X,double Y,double secX,double secY, double R,double rea)
{
R=radius( X, Y, secX, secY, R);
rea= pow(R,2)*3.1416;

return rea;
}
cout <<"Continue? Enter y or n"<<endl;
cin >> c;      // put answer in variable c

tolower('c');  // convert the character c (not the variable) to
// lower case and throw away the conversion
// -- didn't put it in a variable
if (c == y)    // compare the variable c with the variable y --
// what is in y?
main();   // NEVER EVER call main()  It is not a recursive
//  function. Use a loop

And why are you spacing between most lines? It does not make the code more readable.

Alright well I figured that out. It was an issue with my if statements' sytax.

I wasnt using ' ' to signify the characters I wanted the strings to match in the if statements.

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cctype>

using namespace std;
double area(double, double,double,double,double, double,double);
double circ(double, double,double,double,double, double);
double distance(double,double,double,double);
int main()

{double X,secX,Y,secY,z,R, cir,rea;
char c,y,n;

R=0;

cout<< "Please enter the x value of the center of the circle:"<< endl;
cin>>secX;
cout<< "Please enter the y value of the center of the circle:" << endl;
cin>>secY;
cout<< "Please enter the x value of a point on the circle:"<<endl;
cin>>X;
cout<< "please enter the y of said point:"<< endl;
cin>>Y;

cout << fixed << showpoint <<setprecision(2);

cout << "number before being taking square root:"<<pow(X-secX,2)+pow(Y-secY,2)<< endl;

cout <<"distance is = " << distance( X, Y, secX, secY)<< endl;

cout << "Radius is = " << radius( X, Y, secX, secY, R)<< endl;

cout<< "Circumference = "<<  circ(cir, X, Y, secX, secY, R) << endl;

cout << "Area is = " << area( cir, X, Y, secX, secY, R, rea)<< endl;

cout <<"Continue? Enter y or n"<<endl;
cin >> c;

tolower('c');

if (c == 'y')
main();

if (c != 'y')
cout << "Thanks for using the circle calculator!"<<endl;

cout <<c<< endl;

system("PAUSE");
return EXIT_SUCCESS;
}

double distance(double X,double Y,double secX,double secY)
{

return sqrt(pow(X-secX,2)+pow(Y-secY,2));

}
double radius(double X,double Y,double secX,double secY,double R)
{
R=sqrt(pow(X-secX,2)+pow(Y-secY,2));
return R;

}
double circ(double cir, double X,double Y,double secX,double secY, double R)
{
R=radius( X, Y, secX, secY, R);
cir = R*2*3.1416;

return cir;
}
double area(double cir, double X,double Y,double secX,double secY, double R,double rea)
{
R=radius( X, Y, secX, secY, R);
rea= pow(R,2)*3.1416;

return rea;
}

Ahhh you sniped me Walt. Thanks for the targeted questions. I think my intentions are better outlined with the code I just put up. I space the lines because it helps me read it. I am brand spanking new to this stuff. :)

How would I use a loop instead of calling the function main?

By thinking about what you need to accomplish and using a loop to do it. Programming means using your head, not just asking for turn-key solutions to a suggestion. Figure it out. Code it. Then post when you get stuck.

string c,y,n;  //<--- line 16

From what I can see in the rest of your code, there should only be one variable here "c", and it really should be a char, not a string. I'll cover "y" and "n" later.

cout <<"Continue? Enter y or n"<<endl; ??<---line 45
cin >> c;

tolower('c');  //<--- line 48

if (c == y)  //<--- line 50
main();

This section really doesn't accomplish anything beneficial to you. Lines 45 and 46 are okay, they prompt for and accept an input. I see three (3) other problems though:
1. Line 48's net result is nothing except wasted CPU time. It is a good idea, but you didn't implement it correctly. You must store the return value from tolower() and you're not. Also, you need to convert the variable c not the literal character 'c'. Do not use the single-quotes around the name, they completely change the meaning of the statement. You also need to add an assignment statement to the left end of the statement. Without it, the return value is lost and is not of any benefit.

2. The syntax of line 50 is technically correct, but it doesn't match your intent which means the context is not. You are attempting to compare the variables "c" and "y" for equality. This is not correct in this context. You should be comparing the variable "c" with the literal character 'y'. Place single-quotes around your "y" to convert it to a literal.

3. Line 51 NEVER, EVER, EVER, NEVER recursively call main(). You need a loop not a recursion. You should probably read up on control structures, specifically loops.

WOW, I guess I took a little too long to post... WaltP already covered most of it. Your tolower() is still not correct though.

3. Line 51 NEVER, EVER, EVER, NEVER recursively call main().

Just to expand this statement a little and say that calling main in C++ is expressly forbidden by the standard and some compilers will produce an error.

That is one of the many differences between C and C++, calling main in C is not forbidden but is still quite a bad idea.

WOW, I guess I took a little too long to post... WaltP already covered most of it. Your tolower() is still not correct though.

To quote Fran, the Progressive weirdo -- "Happens to me all the time!" :)

To quote Fran, the Progressive weirdo -- "Happens to me all the time!" :)

LOL, nice... :D:P

Thanks for that info about int main() everyone.

Anyways, I just put it all into a recursive function.

That was simple.

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cctype>

using namespace std;
double area(double, double,double,double,double, double,double);
double circ(double, double,double,double,double, double);
double distance(double,double,double,double);
double entire(double,double,double,double,double,double,double,double,char,char);
int main()
{  double X,secX,Y,secY,z,R, cir,rea;
char c,n;

entire( X, secX, Y, secY, z, R, cir, rea, c, n);

system("PAUSE");
return EXIT_SUCCESS;
}

double entire(double X,double secX,double Y,double secY,double z,double R,double cir,double rea,char c,char n)
{

R=0;

cout<< "Please enter the x value of the center of the circle:"<< endl;
cin>>secX;
cout<< "Please enter the y value of the center of the circle:" << endl;
cin>>secY;
cout<< "Please enter the x value of a point on the circle:"<<endl;
cin>>X;
cout<< "please enter the y of said point:"<< endl;
cin>>Y;

cout << fixed << showpoint <<setprecision(2);
cout << "number before being taking square root:"
<<pow(X-secX,2)+pow(Y-secY,2)<< endl;
cout <<"distance is = " << distance( X, Y, secX, secY)<< endl;
cout << "Radius is = " << radius( X, Y, secX, secY, R)<< endl;
cout<< "Circumference = "<<  circ(cir, X, Y, secX, secY, R) << endl;
cout << "Area is = " << area( cir, X, Y, secX, secY, R, rea)<< endl;
cout <<"Continue? Enter y or n"<<endl;
cin >> c;

if ((tolower(c)) == 'y')
entire( X, secX, Y, secY, z, R, cir, rea, c, n);

if (c != 'y')
cout << "Thanks for using the circle calculator!"<<endl;

cout <<c<< endl;
}

double distance(double X,double Y,double secX,double secY)
{      return sqrt(pow(X-secX,2)+pow(Y-secY,2));}
double radius(double X,double Y,double secX,double secY,double R)
{      R=sqrt(pow(X-secX,2)+pow(Y-secY,2));
return R;}
double circ(double cir, double X,double Y,double secX,double secY, double R)
{      R=radius( X, Y, secX, secY, R);
cir = R*2*3.1416;
return cir;}
double area(double cir, double X,double Y,double secX,double secY, double R,double rea)
{
R=radius( X, Y, secX, secY, R);
rea= pow(R,2)*3.1416;

return rea;
}

Wrong answer, you won't be happy when you crash your computer due to lack of memory...

Wait and see what happens when you enter 'n' because you're done.... I can see a WTF!!! coming... :P

Use a loop...

Wrong answer, you won't be happy when you crash your computer due to lack of memory...

Wait and see what happens when you enter 'n' because you're done.... I can see a WTF!!! coming... :P

Use a loop...

I did use a loop?

When I type "n" it says "Thanks for using the circle calculator!" and closes. I dont see the issue.

Recursion is not an answer to your problem. Consider carefully the first thing FBody said.

Call the function (since you already made it one) and instead of calling the function again, just put the function in a loop.

Also you may just wish to consider making some of those function parameters just local variable to the function for example

double radius(double X,double Y,double secX,double secY,double R)
{ R=sqrt(pow(X-secX,2)+pow(Y-secY,2));
return R;}

The value passed in using parameter R is never used, you overwrite R immediately. You are needlessly pass the parameter. This is most obvious at the top level in main where you pass all the parameters to entire without initialising a single one.

This function code be written without parameter R as

double radius(double X,double Y,double secX,double secY)
{ double R=sqrt(pow(X-secX,2)+pow(Y-secY,2));
return R;}

Now it is obvious that R is redundant, correcting the formatting as well

double radius(double X,double Y,double secX,double secY)
{
return sqrt(pow(X-secX,2)+pow(Y-secY,2));
}

Also note that this is now exactly the same as your distance function. That is logical because the distance from the centre of a circle to a point on its circumference is the radius of the circle.

So you are saying I shouldnt have to use teh return values at all? That merely stating the expression will give the desired output?

No I am saying if the value passed in the parameter is irrelivant to the operation of the function then it shouldn't be a parameter in the first place.

Nothing to do with the function return value.

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.