Circle Area & Circumference, please help?

So I have been working on this project for the last few days and have it running except when it returns the radius entered and the circumference and area they are all zeros! What am I doing wrong?

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
 // ==== Header Files ====
#include "stdafx.h"
#include <iostream>
// ======================

// ==== Namespace Uses ====
using namespace std;
// ========================

// ==== Enumerations ====
enum Location {starten, enden};
// ======================

// ==== Constants ====
const double PI = 3.14159265359;
// ===================

// ==== Function Prototypes ====
void Banner(Location);
void ComputeArea(double radius, double& area);
double ComputeCircumference(double radius, double& circum);
void GetValue(double);
bool GoAgain(char);
void OutputData(double radius, double& circum, double& area);
// =============================

// ==== Main Body ====
	int main() {

	// ==== Variables ====
		char answer;
		double circum;
		double radius = 0.0;
		double area;
	// ===================
		
	// ==== Welcome Banner ====
		Banner(starten);
	// ========================

		do{
			GetValue(radius);

			circum = ComputeCircumference(radius, circum);
			ComputeArea(radius, area);

			OutputData(radius, circum, area);

		} while (GoAgain(answer = true));

	// ==== Exit Banner ====
		Banner(enden);
	// =====================

		return 0;
	} // Main Function End
// ===================

// ==== Banner Function ====
	void Banner(Location place) {
		
		cout << endl;

		if (place == starten) {
			cout << "____________________________________" << endl;
			cout << "|           Welcome To:            |" << endl;
			cout << "| Circumfrence Computation Project |" << endl;
			cout << "|      ********************        |" << endl;
			cout << "|      CCP will compute the        |" << endl;
			cout << "| circumference & area of a circle |" << endl;
			cout << "|   that you(user) determine the   |" << endl;
			cout << "|     radius of said circle!       |" << endl;
			cout << "|__________________________________|" << endl;
		} // If Statement
		else {
			cout << "____________________________________" << endl;
			cout << "|        Thank you for using       |" << endl;
			cout << "|              CCP!                |" << endl;
			cout << "|     ************************     |" << endl;
			cout << "|      CCP now terminating...      |" << endl;
			cout << "|__________________________________|" << endl;
		} // Else Statement
		
		cout << endl;

	} // Banner Function End
// =========================

// ==== ComputeArea Function ====
	void ComputeArea(double radius, double& area) {
		
		area = PI * radius * radius;
		
	} // ComputeArea Function End
// ==============================

// ==== ComputeCircumference Function ====
	double ComputeCircumference(double radius, double& circum) {
		
		circum = 2 * PI * radius;
		
		return circum;

	} // ComputeCircumference Function End
// =======================================

// ==== GetValue Function ====
	void GetValue(double radius) {

		cout << "Enter the value of the radius that is to be calculated: ";
		cin >> radius;
		cout << endl;

		if (radius <= 0) {
			cout << "The radius entered is invalid!" << endl;
			cout << endl;
			cout << "The circumference and area cannot be calculated!" << endl;
			cout << endl;
		}

	} // GetValue Function End
// ===========================

// ==== GoAgain Function ====
	bool GoAgain(char answer) {
		
		cout << endl;

		do{
			cout << "Would you like to calculate another circle? (y/n) ";
			cin >> answer;
			cout << endl;

			if (answer == 'y') {
				return true;
			}
			else if (answer == 'n') {
				return false;
			}
			else
				cout << "You have entered an invalid answer! Please try again! " << endl;
				cout << endl;

		} while (answer != 'y', 'n');
		
	} // GoAgain Function End
// ==========================

// ==== OutputData Function ====
	void OutputData(double radius, double& circum, double& area) {
		
		cout << endl;
		cout << "The radius entered is                 ==> " << radius << " " << endl;
		cout << "The circumference of the circle is    ==> " << circum << " " << endl;
		cout << "The area of the circle is             ==> " << area << " " << endl;
		cout << endl;
			
	} // OutputData Function End
// =============================
 
How does the GetValue return the radius to the main()?

PS. The parameter types of your other functions are a bit odd too.
You need to pass the pointer to GetValue(), not the value. Or it needs a return value.
@keskiverto
I was instructed to make GetValue() a void function, not a value returning function. I think that is my biggest issue on this!

Please explain what you mean by:

"PS. The parameter types of your other functions are a bit odd too."

@And G
Thanks for the suggestion! I will implement that ASAP! :)
I think you aren't understanding the use of references as parameters. The result is that the value of "radius" is never set; I will explain why.

When a variable is declared, it is declared within a scope, which can conceptually be seen as a "context". A scope can be a function, or a class, or global, or anything. The scope defines the visibility of a variable. If I do this:

1
2
3
4
5
6
7
8
9
10
 // Variable "myInt" is declared in myFunction() and used in main().
void myFunction() {
    int myInt = 0; // Variable declared
}

int main() {
    myFunction();
    myInt += 1;    // Error! "myInt" undefined in Main.
    return 0;
}

So Main doesn't know about myInt, because it only exists in the scope (context) of myFunction. Similarly, if I do this:
1
2
3
4
5
6
7
8
9
 // Variable "myInt" is declared in main() and used in myFunction().
void myFunction() {
     myInt += 1; // Error! "myInt" undefined in myFunction()!
}
int main() {
    int myInt = 0;
    myFunction();
    return 0;
}


So what you can do, is pass variables from one scope to a lower one by using it as a parameter.
1
2
3
4
5
6
7
8
void myFunction(int myParameter) {
    myParameter += 1;
}
int main() {
    int myInt = 0;
    myFunction(myInt); // Pass myInt as myParameter.
    return 0;
}


Now myFunction can use myParameter. However, literally passing a variable will cause the function to make a local copy of the variable in the scope of that function! Basically, behind the scenes it does this:
1
2
3
4
void myFunction(int myParameter) {
    int myLocalCopy = myParameter;
    myLocalCopy += 1;
}

This means myInt is never changed, because the function doesn't actually have access to it; just the local copy! In fact, none of the myFunction() definitions above will have any effect, because only local changes are made and never fed back into the calling scope (here: main).

There are two main ways of making a function (a "called" scope) influence the caller (the "calling" scope). One is by returning a value.
1
2
3
4
5
6
7
8
9
10
void myFunction(int myParameter) {
    myParameter += 1;
    return myParameter;
}

int main() {
    int myInt = 0;
    myInt = myFunction(myInt);
    return 0;
}


This is useful and is in many cases the easiest/best/fastest/sexiest way of having a function communicate back to the calling scope. However, sometimes the situation is more complex. There are many possible reasons, but lets just say that you need a function to return 2 values instead of just one. C++ doesn't support multiple return values. However, you can give a function the permission to change the original variable that is passed to it as a parameter, by passing that variable by reference.

1
2
3
4
5
6
7
8
9
10
11
12
void myFunction(int& myFirstParameter, int& mySecondParameter) {
    myFirstParameter += 1;
    mySecondParameter -= 1;
}

int main() {
    int myFirstInt = 0;
    int mySecondInt = 10;
    myFunction(myFirstInt, mySecondInt);
    // Values are now 1 and 9 respectively!
    return 0;
}



Now see if you can apply this to your code. The problem variable is "radius", which is not being read in and therefore keeps its default value.

[edit] In case it's not clear: the "reference" definition comes by adding the ampersand in the parameter. Notice the 'int' versus 'int&'. 'int' parameters are passed by value (= copied), 'int&' parameters are passed by reference (= changeable [unless const]).

Also, as @And G said, you can pass variables by pointer. The logic is the same, but the syntax is messier. References are [in my opinion] a much cleaner way of getting this behavior than pointers. I think a solid rule for C++ beginners is "if you can do it by reference, do it by reference; otherwise use pointers".
Last edited on
Thanks for all the help!
I got it running! :)

@Gaminic you were most helpful! Thank you so much!
Topic archived. No new replies allowed.