problem with a mathematical program

closed account (2wv7M4Gy)
I wrote my self a program but it doesn't work :

#include <iostream>
#include <math.h>

using namespace std;
int main()
{
float r , p , s;
float P=3.14;

do
{
cout<<"r=";
cin>>r;
if (r<=0);
cout<<"Can't accept negative numbers or 0 "<<endl;
}
while(r<=0);

p=2*P*r;
s=P*r^2;

cout<<"The perimeter is : " <<p<<endl;
cout<<"The area is : " <<s<<endl;

system ("pause");
return 0;
}

what's the problem ? if anyone could help me i would appreciate it !
1
2
3
4
5
6
7
8
9
10
11
while(r<=0); //this is the error, it should be (r>0)

p=2*P*r;
s=P*r^2;

cout<<"The perimeter is : " <<p<<endl;
cout<<"The area is : " <<s<<endl;

system ("pause");
return 0;
}


you were taking the criteria so that it would work only when the radius is less than 0 or 0, try it that way
closed account (2wv7M4Gy)
Still doesn't work despite toxic's advice!!!!!!!!!!!!
Last edited on
You should remove the semicolon after the if condition and the ^ operator is xor in C++, use instead pow():
s=P*pow(r,2);

(edit: Toxic90 advice is wrong)
Last edited on
You shouldn't use pow() to calculate the square (or, for that matter, any integer exponent). It most certainly is pretty slow and it is inexact (no guarantee whatsoever about the result, as opposed to (d*d) which will be the exact value of the multiplication rounded to the nearest existing double value).
Last edited on
Topic archived. No new replies allowed.