you've forgotten to group the code propperly. What you wrote is basically
Code:
#include <iostream>
using namespace std;
#define PI 3
int main()
{
double r, d, circle, entered_number;
cout<<"Enter 1 to find circumference, or enter 2 to find radius/diameter"<<endl;
if (entered_number == 1) {
cout<<"To find circumfrence of circle, please input radius"<<endl;
}
cin>>r;
circle = PI * r * 2;
cout<<"The circumfrence of the circle is "<<circle<<endl;
else if (entered_number == 2) {
cout<<"To find radius of circle, please input circumfrence"<<endl;
}
cin>>circle;
r = circle/(PI*2);
d = r * 2;
cout<<"The radius of the circle is "<<r<<" and diameter is "<<d<<endl;
return 0;
}
When you wanted
Code:
#include <iostream>
using namespace std;
#define PI 3
int main()
{
double r, d, circle, entered_number;
cout<<"Enter 1 to find circumference, or enter 2 to find radius/diameter"<<endl;
if (entered_number == 1) {
cout<<"To find circumfrence of circle, please input radius"<<endl;
cin>>r;
circle = PI * r * 2;
cout<<"The circumfrence of the circle is "<<circle<<endl;
} else if (entered_number == 2) {
cout<<"To find radius of circle, please input circumfrence"<<endl;
cin>>circle;
r = circle/(PI*2);
d = r * 2;
cout<<"The radius of the circle is "<<r<<" and diameter is "<<d<<endl;
}
return 0;
}
If you want more than one block of code after an if/else/for/while/whatever statement, you need to enclose it in brackets. Also, it's common to indent blocks so that the end result looks like this (but it's strictly optional, C/C++ ignores excess white space)
Code:
#include <iostream>
using namespace std;
#define PI 3
int main()
{
double r, d, circle, entered_number;
cout<<"Enter 1 to find circumference, or enter 2 to find radius/diameter"<<endl;
if (entered_number == 1) {
cout<<"To find circumfrence of circle, please input radius"<<endl;
cin>>r;
circle = PI * r * 2;
cout<<"The circumfrence of the circle is "<<circle<<endl;
} else if (entered_number == 2) {
cout<<"To find radius of circle, please input circumfrence"<<endl;
cin>>circle;
r = circle/(PI*2);
d = r * 2;
cout<<"The radius of the circle is "<<r<<" and diameter is "<<d<<endl;
}
return 0;
}
Makes the code much cleaner, as you can see.
Bookmarks