# Circle area

Gold Member

## Main Question or Discussion Point

What's wrong with this C++ project I'm working on? it's supposed to find the area of a circle:

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int pi = 3.14
cout << "radius of the circle:" <<

system("PAUSE");
return EXIT_SUCCESS;
}

Related Computing and Technology News on Phys.org
LeonhardEuler
Gold Member
It's been a while since I've done programming, but looking at it quickly, I just notice that you said int pi =3.14, but int only stores integers so your really saying int pi =3 . This may not be the only problem, but its the one that I see.

dduardo
Staff Emeritus
Ahh what a mess!!! Try this:

Code:
double pi = 3.14 ;
cout << "radius of the circle:" ;
cout << "The radius of the circle is about:" << rad*rad*pi << endl ;

Last edited:
Gold Member
oh, I meant 'the area of the circle is about:'

but thanks.

LeonhardEuler
Gold Member
Oh, and you square rad before it gets the input from the user. That means that since rad starts out as random garbage before you give it a value, radsqr is random garbage squared. You have to square it after the line that says cin<<... to get what you want. So if you make that change and change the int's to float's it should work.

Gold Member
grrr....Now I can't get the circumference to show up:
Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{

double pi = 3.14 ;
cout << "radius of the circle:" ;
cout << "The circumference of the circle is about:" << rad*2*pi <<
system("PAUSE");
return EXIT_SUCCESS;
}
it only displays the area

dduardo
Staff Emeritus
Actually you probably want rad to be double too since you can have decimal values.

Why do you have a hanging << at the end of the circumference line? Dont' forget the endl;

Gold Member
it says:

stupid dev C++ said:
end1' is undeclared (first use this function)
:grumpy:

LeonhardEuler
Gold Member
It's endl, not end1(end-lowercase L, not end-one)

dduardo
Staff Emeritus
Its the letter "L", but lower case. Not the number "1"

Gold Member
oh, I'm stupid. It's the font on my compiler that's confusing me

Gold Member
my formula calculation is complete:

The area of the circle is about: 28.26
The circumference of the circle is about: 18.84
The diameter of the circle is: 6

muahaha

dduardo
Staff Emeritus
Congrats! Now try something more difficult like calculate pascal's triangle.

Gold Member
I'll work my way up.

what was pascal's triangle? the 3/4/5 one? in trig?

Gold Member
aha! basic trig

Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
double hyp
double opp
cout << "Input sides of a right triangle
cout << "Hypotenuse:" << endl;
cin >> hyp
cout << "opposite" << endl;
cin >> opp
cout << "Triangle's properties are:" << endl;
system("PAUSE");
return EXIT_SUCCESS;
}

dduardo
Staff Emeritus
Gold Member
unfortunately, I do not understand that

The flaw in my trig program is that anyone can put the hypotenuse as a smaller length than the opposite or adjacent sides. could I do an if statement that if this happens, function main() will execute again?

dduardo
Staff Emeritus
Or you could just have a loop that continues to ask for the hypotenuse until it is an accepatable value.

Gold Member
something like?

{
cin >> hyp
}

dduardo
Staff Emeritus
A do while loop would be appropriate:

do {
cin >> hyp ;
} while ( (hyp < opp) || (hyp<adj) ) ;

Gold Member
okay, and where would I put it in the code? right after sin cos and adj are specified, right?

dduardo
Staff Emeritus
Well, in order to compare hyp you need to know the value of opp and adj, so after you get those to values then you can stick the above code in.

Programming is a logical process. Whatever the flow of the code, it has to make sense. You can't just be comparing variables that don't have any specified value.

Gold Member
now it says that there should be a semicolon before the variable declaration, like:

;double

and it says that on that same line, there's a missing terminating character.

Code:
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
double hyp
double opp
cout << "Input sides of a right triangle
cout << "Hypotenuse:" << endl;
cin >> hyp
cout << "opposite" << endl;
cin >> opp
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
double hyp
double opp
cout << "Input sides of a right triangle
cout << "Hypotenuse:" << endl;
cin >> hyp
cout << "opposite" << endl;
cin >> opp
cout << "Triangle's properties are:" << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
cout << "Triangle's properties are:" << endl;
system("PAUSE");
return EXIT_SUCCESS;
}`

arildno
Homework Helper
Gold Member
Dearly Missed
In order to get a better precision in your pi-value than 3.14, a common strategy is to define your pi as follows:

double pi=4*atan(1);

Where atan is the arcus-tangent function.