Help me simplify my class assignment

I'm in a C++ class and have completed my assignment. Everything works correctly, but I was hoping for some tips or anything I could do to make it more streamlined.

Thanks for the help - James

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;
int main()
{
const double pi=3.14159;
double radius, circumference, area;

cout<<"Enter the radius of the circle: ";
cin>> radius;

circumference= pi*radius;
area= pi*(pow(radius,2));

cout<< fixed;
cout<< setw (6) << "Radius"
<< setw (16)<< "Circumference"
<< setw (7) << "Area" << endl;
cout<< setw (6) << setprecision (2) << right << radius
<< setw (14)<< setprecision (2) << right << circumference
<< setw (9) << setprecision (2) << right << area << endl;

return 0;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>
#include <cmath>
#define OUT(i) setw(i) << setprecision(2) << right

int main()
{
	const double pi = 3.14159;
	double radius;

	cout << "Enter the radius of the circle: ";
	std::cin >> radius;

	double circumference = pi*radius, area = pi*(pow(radius,2));

	std::cout << fixed << setw(6) << "Radius" << setw(16) 
		<< "Circumference" << setw(7) << "Area" << std::endl;
	std::cout << OUT(6) << radius << OUT(14) << pi*radius << OUT(9) 
		<< pi*(pow(radius,2)) << std::endl;
	return 0;
}

There really isn't much you can do here. Here's what I did, but it isn't much at all.

EDIT: Yeah, I somewhat copied vlad on this one.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>
 
using namespace std;

int main()
{
   const double pi = 3.14159;
   double radius;
 
   cout << "Enter the radius of the circle: ";
   cin>> radius;
 
   cout << fixed;
   cout << setw( 6 )  << "Radius"
        << setw( 16 ) << "Circumference"
        << setw( 7 )  << "Area" << endl;
   cout << setprecision( 2 ) << right 
        << setw( 6 )  << radius
        << setw( 14 ) << pi * radius
        << setw( 9 )  << pi * radius * radius << endl;
} 
Last edited on
I actually had this same assignment today. It's not quite as streamlined as some of the others, but it is pretty easy to follow I think and is a pretty simple way to do it.

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

// Find the area or circumference of a circle

#include <iostream>
using namespace std;
 
 int main() {
 
	cout << "I will tell you the area or circumference of your circle." << endl;
	cout << "All I need is the radius." << endl;
	cout << "First tell me what you would like me to do." << endl;
	
	bool keep_going = true;
	
	
	while(keep_going == true) {
		int choice;
		float radius;
		
	
	// Prompts the user for which value they wish to compute.	
		cout << endl << "Enter '1' for the area" << endl;
		cout << "Enter '2' for the circumference. " << endl;
		cout << "Enter '0' to exit this program." << endl;
		cin >> choice;
		
		if(choice == 1 || choice == 2){
			
			cout << endl << endl << "What is the radius of your circle: ";
			cin >> radius;
			}
			// Gets the area of the circle.
			if (choice == 1){
				cout << "The area of your circle is: " << radius * radius * 3.14159 << endl;
				keep_going = true;
				}
			
			// Get the circumference of the circle.	
			else if (choice == 2) {
				cout << "The circumference of your circle is: " << radius * 2 * 3.14159 << endl;
				keep_going = true;
				}
			
			// Ends the program if something other than 1 or 2 is entered. 
			else if (choice == 0) {
				keep_going = false;
				}
			
		}
	
	return 0;
	}
Last edited on
You can do simply.


// Find the area or circumference of a circle

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
#include <iostream>
using namespace std;
 
 int main() 
{
 
	cout << "I will tell you the area or circumference of your circle." << endl;
	cout << "All I need is the radius." << endl;
	cout << "First tell me what you would like me to do." << endl;
	
	
	int choice;

	do
	{
		float radius;
		
	
	// Prompts the user for which value they wish to compute.	
		cout << endl << "Enter '1' for the area" << endl;
		cout << "Enter '2' for the circumference. " << endl;
		cout << "Enter '0' to exit this program." << endl;
		cin >> choice;
		
		if(choice == 1 || choice == 2){
			
			cout << endl << endl << "What is the radius of your circle: ";
			cin >> radius;
			}
			// Gets the area of the circle.
			if (choice == 1){
				cout << "The area of your circle is: " << radius * radius * 3.14159 << endl;
				}
			
			// Get the circumference of the circle.	
			else if (choice == 2) {
				cout << "The circumference of your circle is: " << radius * 2 * 3.14159 << endl;
				}
		}
			
			
	} while ( choice );
	
	return 0;
	}
Last edited on
Reply to: mhaggard

Is it unnecessary to continually redefine keep_going as true when you can simply allow it to loop until you define it as false. The value is set to true at the beginning and if it wasn't true the program wouldn't be reading in the loop so it is just unnecessary to define it as true within the loop.
Reply to: Ryan Boykin

Ok thanks! Now that I'm a little further into the semester with it I noticed it. I appreciate the advice though. Everything I can learn certainly helps.
Topic archived. No new replies allowed.