HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Simple C++ function to determine the circumference and area of a circle

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
circlethesimplefunctioncircumferencedetermineandarea

Problem

First off I would like to state that this is a homework assignment. However, I am not looking for you to complete anything for me. I code I am posting is the completed homework assignment. However, since this is my first time using C++ I was curious if there might have been a better method to accomplish the same task.

/*
 * PROJECT: Week 1 - Homework (Circle Calculations)
 * VERSION: 0.0.0 as of 201210104
 *
 * DESC:
 * In this homework assignment you will create a program that
 * will calculate the area and circumference of a circle.
 *
*/

#include
using std::cout;
using std::cin;
using std::endl;

#include;

bool main(){

    const double PI = 3.14;
    double radius;
    double circumference;
    double area;

    // display backing
    cout > radius;      // except input

    // do the math
    circumference = PI * 2 * radius;    // circumference of a circle
    area = PI * pow(radius,2.0);        // area of a circle

    // display answer
    cout << "\nA circle with a radius of " << radius << endl;
    cout << "has a circumference of: " <<  circumference << endl;
    cout << "has an area of: " << area << "\n" << endl;

    system("pause");

    return true;
}


Here is the outcome of all changes I have made to the code above. I was able to solve how to handle an input other than of the the required data type.

```
/*
* PROJECT: Week 1 - Homework (Circle Calculations)
* VERSION: 1.0.0 as of 201210106
*
* DESC:
* In this homework assignment you will create a program that
* will calculate the area and circumference of a circle.
*
*/

#include
#include
#include
#include
using namespace std;

bool GetInt(float & n)
{
string str;
getline(cin,str);

stringstream buffer(str);
buffer >> n;

if (!buffer)
{
cout > radius)){// loop if not cin returns false
// display the error message
cout ::max() - returns the max length of the
cin streamsize.

cin.ignore(nu

Solution

Extremely good for a first time at C++! A few things though:

  • #include should be #include



  • Same with #include



  • Also, no need for the ; after the cmath include



  • main does not have a return type of bool. See this or this.



  • I would put the using declarations after all includes. It should be safe, but no need to pull things into the global namespace until the last minute.



  • cin >> radius; doesn't check for failure or success (see note below)



  • In your final output, you used all endl and then have a random \n



  • \n and endl are not functionality-wise equivalent. In this situation though, that difference is not going to matter. I suggest you either stick with all endl or have all \n and then a final endl (I would probably use all endl just for the consistency). endl is essentially equivalent to writing a newline and flushing



  • I'm not a fan of system("pause");



  • It's system dependent. (The 'pause' part, not the system part.)



  • And, in my opinion, your program has no reason to remain running once it's done. Leave it up to the user whether or not to leave the prompt open.



  • Additionally, though it seems like system("pause"); is a quick little solution, it may be better to just block on reading any character if you're determined to leave the application running. system seems a bit overkill.



Note about cin >> radius;

The typical approach with handling istreams is to use the extraction as a bool:

if (cin >> radius) {
    //A double was successfully read
} else {
    //A double was not successfully read
}


To be honest, I'm not actually sure what the idiomatic way to force a user to provide a valid input is. Hopefully one of the (much) more knowledge C++ regulars will comment on that. My guess though would be to use ignore() to clear out cin's buffer and try to grab a new input.

Code Snippets

if (cin >> radius) {
    //A double was successfully read
} else {
    //A double was not successfully read
}

Context

StackExchange Code Review Q#16185, answer score: 7

Revisions (0)

No revisions yet.