Hello fellow community. I am currently a total beginner and in the fall moving onto college to begin my degree in Computer Science. I am, however, having some trouble with getting my code to work correctly and I was wondering if you could just quickly review it and tell me if it looks good :)
#include <iostream>
usingnamespace std;
int main()
{
cout << "Hello! This simple program is designed to find the \n";
cout << "area and circumference of a circle. \n\n";
cout << "Please enter the radius of the circle: \n";
int radius;
cin >> radius;
float Pi = 3.14159;
float area = radius * radius * Pi;
float circumference = 2 * Pi * radius;
cout << "The area of the circle is: " << area << endl;
cout << "The circumference of the circle is: " << circumference << endl;
return 0;
}
cout << "Please enter the radius of the circle:" << endl;
"endl" basically means to add a new-line character and then ensure that everything you've send to the screen actually gets printed; this is called flushing the stream or just plain flushing.
In mathematics, PI is a mathematical constant; this logic should be conveyed in your code. So on line 15, I would make "Pi" constant.
As for your code, it's clean, readable, and makes sense. Well done!
#include <iostream>
// using namespace std; // do uou really need this?
int main()
{
/*
cout << "Hello! This simple program is designed to find the \n";
cout << "area and circumference of a circle. \n\n";
cout << "Please enter the radius of the circle: \n";
*/
// we can combine these into one string
std::cout << "Hello! This simple program is designed to find the \n""area and circumference of a circle. \n\n""Please enter the radius of the circle: \n";
int radius; // should this be int or double?
std::cin >> radius;
//float Pi = 3.14159;
constdouble Pi = 3.14159; // Pi is const; and prefer double over float
/*float*/ constdouble area = radius * radius * Pi;
/*float*/ constdouble circumference = 2 * Pi * radius;
/*
cout << "The area of the circle is: " << area << endl;
cout << "The circumference of the circle is: " << circumference << endl;
*/
// in general, avoid flushing the stream unless it is essential
std::cout << "The area of the circle is: " << area << '\n'
<< "The circumference of the circle is: " << circumference << '\n' ;
// return 0; // this can be omitted; return 0 is implied
}
> Some compilers (i.e mine) g++ refuse to compile if you omit return 0
It is not a good idea to use compilers which are fifteen years old.
> It's also good practice to clarify that the program has finished well.
It is an even better practice not to say anything when there is nothing interesting to say; hence the implicit return 0 ;, in both C (last 13 years) and C++ (always).