Review my small amount of code?

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 :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace 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;
}
Looks fine, what problem are you having ?
closed account (zb0S216C)
On line 10, I would make a small change:

 
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!

Wazzak
Last edited on
I am just looking for constructive criticism to see if there was anything that I either did wrong or could do better.

Right now I am trying to make it so that if the user presses 1, the process can be repeated.
Thank you Framework! That is exactly what I was looking for!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#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;
    const double Pi = 3.14159; // Pi is const; and prefer double over float

    /*float*/ const double area = radius * radius * Pi;
    /*float*/ const double 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
}
closed account (3CXz8vqX)
Some compilers (i.e mine) g++ refuse to compile if you omit return 0; It's also good practice to clarify that the program has finished well.
Last edited on
> 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).
Topic archived. No new replies allowed.