Calculations in cout

Apr 20, 2012 at 6:05am
I am trying to figure out how to calculate a formula in my cout line and then display the answer. Could someone help me figure this out?
Apr 20, 2012 at 6:08am
What is the desired formula?
Apr 20, 2012 at 6:12am
The formula is:

diameter = radius + radius

I have this as the cout

cout << "The diameter of the circle is : " << diameter << endl << endl;

I just need to get the formula into the cout and I am at a loss on how to do this.
Apr 20, 2012 at 6:14am
I need to see your code to help you.
Apr 20, 2012 at 6:16am
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

#include <iostream>
#include <string>

using namespace std;

float pi = 3.1415926;

void main () {

	// input variables

	float radius;

	// processing variables

	float diameter,
		circumference,
		area;

	// input section

	cout << "This program will take the radius of the circle that you enter and give you the diameter, circumference, and area of the circle." << endl << endl;

	cout << "Please enter the radius of the circle: ";
	cin >> radius;

	// processing section


	 diameter = radius + radius;

	 area = radius * radius * pi;
	
	circumference = pi * diameter ;

	// output section

	cout << "The diameter of the circle is : " << diameter << endl << endl;

	cout << "The area of the circle is : " << area  << endl << endl;

	cout << "The circumference of the circle is : " << circumference << endl;

	system ("pause");


}
Apr 20, 2012 at 6:25am
The code compiles. What happens when you run it?
Apr 20, 2012 at 6:27am
I get it to work great this way. I just need them to do the calculations in the output statment
Apr 20, 2012 at 6:32am
This program will take the radius of the circle that you enter and give you the
diameter, circumference, and area of the circle.

Please enter the radius of the circle: 10.5
The diameter of the circle is : 21

The area of the circle is : 346.361

The circumference of the circle is : 65.9734
Press any key to continue . . .

are you not getting this when you run it?
Apr 20, 2012 at 6:36am
I do. The program itself works correctly. The problem i am having is i need to move the formulas into the cout kind of like this :

cout << diameter = radius + radius << "The diameter of the circle is : " << diameter << endl << endl;

But that will cause errors and I do not know how to make it work with the formulas within the output statement.
Apr 20, 2012 at 6:43am
Try
cout << diameter << " = " << radius << " + "<< radius<< ...;
Apr 20, 2012 at 6:46am
I guess I should have been more specific i need it to preform the calculation and then display the answer. I do not need to show the formula itself. Sorry for the confusion.
Apr 20, 2012 at 7:13am
I ended up figuring it out. Thank you for your time in trying to help me. It was greatly appreciated.


Here is the final code :

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

#include <iostream>
#include <string>

using namespace std;

float pi = 3.1415926;

void main () {

	// input variables

	float radius;

	// processing variables

	float diameter,
		circumference,
		area;

	// input section

	cout << "This program will take the radius of the circle that you enter and give you the diameter, circumference, and area of the circle." << endl << endl;

	cout << "Please enter the radius of the circle: ";
	cin >> radius;

	
	// output section

	cout << "The diameter of the circle is : " << radius + radius << endl << endl;

	cout  << "The area of the circle is : " << radius * radius * pi << endl << endl;

	cout << "The circumference of the circle is : " << pi * ( radius + radius ) << endl;

	system ("pause");


}
Apr 20, 2012 at 10:43pm
You are welcome! I am gladd you got it!
Topic archived. No new replies allowed.