• Forum
  • Lounge
  • Looping - Diameter, Circumference, and A

 
Looping - Diameter, Circumference, and Area

Hey everyone! I'm really, really desperate now. My prof in Computer Science is of no help and is really rude so I'm stuck here not knowing what I did wrong.

Our assignment is:
Write a complete and correct C++ program that calculates and prints
the diameter, the circumference, or the area of a circle, given the
radius.

I have written a code but it won't run! I don't understand anymore... can someone help me?

#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

int main()
{
//Declare the variables used
float area, circumference, areaOfCircle, pi, diameter;
bool getNumber = true;
char letter;
int r;
pi = 3.14159265;

while (true) //Enable the program to loop
{

//Prompt the user to type in a letter and a number
cout <<"Enter A for Area, C for Circumference, D for Diameter, Q to Quit; Followed by the radius: " << endl;
cin.get(letter); //Input any letter for option

//Will terminate if Q is typed
if (letter == 'Q')
{
cout << "Terminating as requested." << endl;

return 0; //Stop
}

while(getNumber)
{
if (!(cin >> r)
{
cout <<"Please enter numbers only." << endl;
cin.clear();
cin.ignore(10000, '\n');
}

else if getNumber = false;

if(r > 0)
{
if(letter == 'A')
{
areaOfCircle = pi*r*r;
cout <<"The area of the circle is" << fixed << setprecision(5) << r << "is" << fixed << setprecision(5) << areaOfCircle << endl;
}

else if (letter == 'D')
{
diameter = 2*r;
cout <<"The diameter of a circle with a radius" << fixed << setprecision(5) << r << "is" << fixed << setprecision(5) << diameter << endl;
}

else if (letter == 'C')
{
circumference = 2*pi*r;
cout <<"The circumference of a circle with a radius" << fixed << setprecision(5) << r << "is" << fixed << setprecision(5) << circumference << endl;
}

else
{
cout <<"No calculation performed. Invalid letter." << endl << endl;
}
}

else
{
cout <<"No calculation performed. Radius must be positive." << endl;
}
}
}
The only real problems are syntax errors preventing it from compiling, and a couple logic errors preventing it from running correctly. I have fixed both in the following code, please see the comments I added explaining the problems.
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

int main()
{
    //Declare the variables used
    float area, circumference, areaOfCircle, pi, diameter;
    int r;
    pi = 3.14159265;

    while (true) //Enable the program to loop
    {
        char letter;
        bool getNumber = true;//<---- moved into loop to re-initialize true,
                              //      or the while(getNumber) loop only runs once

        //Prompt the user to type in a letter and a number
        cout << "Enter A for Area, C for Circumference, D for Diameter, Q to Quit; Followed by the radius: " << endl;
        cin >> letter;// cin.get(letter); //Input any letter for option

        //Will terminate if Q is typed
        if (letter == 'Q')
        {
            cout << "Terminating as requested." << endl;

            return 0; //Stop
        }

        while (getNumber)
        {
            if (!(cin >> r))//<---- missing closing parenthesis
            {
                cout << "Please enter numbers only." << endl;
                cin.clear();
                cin.ignore(10000, '\n');
            }

            else getNumber = false;//<----- else if with no condition

            if (r > 0)
            {
                if (letter == 'A')
                {
                    areaOfCircle = pi*r*r;
                    cout << "The area of the circle is" << fixed << setprecision(5) << r << "is" << fixed << setprecision(5) << areaOfCircle << endl;
                }

                else if (letter == 'D')
                {
                    diameter = 2 * r;
                    cout << "The diameter of a circle with a radius" << fixed << setprecision(5) << r << "is" << fixed << setprecision(5) << diameter << endl;
                }

                else if (letter == 'C')
                {
                    circumference = 2 * pi*r;
                    cout << "The circumference of a circle with a radius" << fixed << setprecision(5) << r << "is" << fixed << setprecision(5) << circumference << endl;
                }

                else
                {
                    cout << "No calculation performed. Invalid letter." << endl << endl;
                }
            }

            else
            {
                cout << "No calculation performed. Radius must be positive." << endl;
            }
        }
    }
}//<----- no closing brace 


Edit:
If you have any questions about my changes feel free to ask. I should also mention, questions like this should probably go in the beginners or general forum, you will most likely get more help there in the future.
Last edited on
Topic archived. No new replies allowed.