/*Make a program that allows the user to input either the radius, diameter, or area of the circle. 
The program should then calculate the other 2 based on the input. (Beginner)*/

#include <iostream>
using namespace std;
#include <cmath>

int main ()
{
    double pi = 3.14159265, radius, diameter, area;
    char choice;
    bool quit = false;

    do
    {
        cout << "Enter D for diameter, R for radius, or A for area: ";
        cin >> choice;

        if (choice == 'D' || choice == 'd')
        {
            cout << "Enter the value for the diameter of your circle: ";
            cin >> diameter;
            cout << endl;

            radius = diameter/2;
            area = pi * radius;

            cout << "Diameter: " << diameter << endl
                << "Radius: " << radius << endl
                << "Area: " << area << endl;
            cout << "Would you like to continue? (Y/N): ";
            cin >> choice;

            if (choice == 'Y' || choice == 'y')
            {
                cout << endl;
                continue;
            }
            else
                quit = true;
        }
        else if (choice == 'R' || choice == 'r')
        {
            cout << "Enter the value for the radius of your circle: ";
            cin >> radius;
            cout << endl;

            area = pi * radius;
            diameter = radius * 2;

            cout << "Diameter: " << diameter << endl
                << "Radius: " << radius << endl
                << "Area: " << area << endl;
            cout << "Would you like to continue? (Y/N): ";
            cin >> choice;

            if (choice == 'Y' || choice == 'y')
            {
                cout << endl;
                continue;
            }
            else
                quit = true;
        }
        else if (choice == 'A' || choice == 'a')
        {
            cout << "Enter the area of your circle: ";
            cin >> area;
            cout << endl;

            diameter = sqrt((4*area)/pi);
            radius = diameter/2;

            cout << "Diameter: " << diameter << endl
                << "Radius: " << radius << endl
                << "Area: " << area << endl;
            cout << "Would you like to continue? (Y/N): ";
            cin >> choice;

            if (choice == 'Y' || choice == 'y')
            {
                cout << endl;
                continue;
            }
            else
                quit = true;
        }
    }while (quit == false);

    cout << endl;

    cin.get();
    return 0;
}

This is my completed program to one of the practice programs given in the sticky at the top of the forums. In the book I am learning from it states that using the "continue" and "break" keywords shoudl be avoided when possible. Is there a way to have this run without using "continue"? Also, do any of you have any suggestions as to making this program any better? (i.e. additions, or better way to code). Thanks again for all of you guys help. :)

Also, sorry about the coloring of the text. It does it automatically when I copy and paste between code tags. I also found out that in order for my text to stay indented I have to use firefox instead of ie7.

Recommended Answers

All 9 Replies

Member Avatar for iamthwee

>Is there a way to have this run without using "continue"?

Yes

>I also found out that in order for my text to stay indented I have to use firefox instead of ie7.

:lol:

I don't think using there is anything wrong with using continue and break keywords -- though there is a lot of controversy surrounding goto statement. (which is extremely biased since even goto is good if used properly). Well the only thing I can think against break , continue or goto is that they break the program flow and result in unstructured program flow....

In your program you can easily eliminate the need for continue by checking otherwise of what you are currently doing i.e.

if( tolower( choice ) == 'n' )
{
    quit = true ;
}

Though I really would recommend using a switch statement and delegating your computing logic to functions which would be in turn called inside your switch construct.

Also if you will notice, there is a lot of repeating code in your program. I would leave the "removal of repetitive code" entirely to you... I have given you the hint. ;)

Also I believe it would be better if you initialized your variables while declaring them to avoid some subtle bugs. Any reason you are hardcoding the value of PI when there are better ways of achieving the same effect....

In the book I am learning from it states that using the "continue" and "break" keywords shoudl be avoided when possible.

Your code should flow logically so that people can read it and understand it easily. The whole single entry single exit dealie from structured programming principles is too strict for real code. I've seen programs that follow those guidelines religiously and it's a scary sight. ;)

Is there a way to have this run without using "continue"?

Yes, just delete the continue statements. They're the last statement before an implicit continue anyway, and you don't need them.

Also, do any of you have any suggestions as to making this program any better? (i.e. additions, or better way to code).

Remove redundancy wherever you can. You have three occurrences of printing results and prompting to continue when you only need one for example :)

Thanks everyone. I have gotten rid of most redundancy(I think). I have also gotten rid of the "continue" keyword.

... Any reason you are hardcoding the value of PI when there are better ways of achieving the same effect....

How would I enter the value of pi without declaring it first?

/*Make a program that allows the user to input either the radius, diameter, or area of the circle. 
The program should then calculate the other 2 based on the input. (Beginner)*/

#include <iostream>
using namespace std;
#include <cmath>

int main ()
{
    double pi = 3.14159265, radius, diameter, area;
    char choice;
    bool quit = false;

    do
    {
        cout << "Enter D for diameter, R for radius, or A for area: ";
        cin >> choice;

        if (choice == 'D' || choice == 'd')
        {
            cout << "Enter the value for the diameter of your circle: ";
            cin >> diameter;
            cout << endl;

            radius = diameter/2;
            area = pi * radius;
        }

        else if (choice == 'R' || choice == 'r')
        {
            cout << "Enter the value for the radius of your circle: ";
            cin >> radius;
            cout << endl;

            area = pi * radius;
            diameter = radius * 2;
        }
        
        else if (choice == 'A' || choice == 'a')
        {
            cout << "Enter the area of your circle: ";
            cin >> area;
            cout << endl;

            diameter = sqrt((4*area)/pi);
            radius = diameter/2;
        }

            cout << "Diameter: " << diameter << endl
                << "Radius: " << radius << endl
                << "Area: " << area << endl;
            cout << "Would you like to continue? (Y/N): ";
            cin >> choice;

            if (choice == 'Y' || choice == 'y')
            {
                cout << endl;
            }
            else
                quit = true;
        
    }while (quit == false);

    cout << endl;

    cin.get();
    return 0;
}

Any reason you are hardcoding the value of PI when there are better ways of achieving the same effect....

I don't get what you are saying. Are you saying it's better to calculate pi? I have never seen a program that calculates pi for formulas. Heck, even calculators store it as a constant! (Unless that's what you meant...)

I don't get what you are saying. Are you saying it's better to calculate pi? I have never seen a program that calculates pi for formulas. Heck, even calculators store it as a constant! (Unless that's what you meant...)

I don't think pi is going to change any time soon, but perhaps ~s.o.s~ meant that using a constant rather than a local variable is to be preferred. In that I would agree, as there's little point in using a variable to hold a constant value. :)

I don't think pi is going to change any time soon, but perhaps ~s.o.s~ meant that using a constant rather than a local variable is to be preferred. In that I would agree, as there's little point in using a variable to hold a constant value. :)

I was aware of that possibility, but maybe better wording would have helped. (Such as "why are you using a variable to store pi when it never changes... *hint*" ;))

Yes Raye that's what I meant, thanks for clarifications in my absence.

It doesn't make sense to make PI a local variable when you are better off writing it as a constant. And as far as academic and small time purposes are concerned, I would write something like: const double PI = 22 / 7.0 ; .

Hassle free (you don't have to keep writing all those decimal numbers) and makes much more sense. Though in real life you would be using a value of PI hardcoded somewhere in one of your header files having much more precision. (Its a pity cmath library doesn't have this like Java and other languages do)

I was aware of that possibility, but maybe better wording would have helped. (Such as "why are you using a variable to store pi when it never changes... *hint*")

So in short, pulling my leg, eh.. ? :D

Or possibly calculate it to initialize a const value once using atan or somesuch. That way you can be fairly sure that your copy of pi is at the implementation's best ability portably.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.