#include "stdafx.h"
#include <iostream>
usingnamespace std;
void banner();
void getValue (double&);
double computeCircumference(double);
void computeArea(double, double&);
void outputData(double, double, double);
bool goAgain();
int main(){
double radius = ' ';
double area = ' ';
banner();
do{
getValue(radius);
double circumference = computeCircumference (radius);
computeArea (radius, area);
outputData (radius, circumference, area);
} while ( goAgain() );
return 0;
}
void banner(){
cout << "======================================================" << endl;
cout << "--------------Welcome to Project Circle!--------------" << endl;
cout << "------------------------------------------------------" << endl;
cout << "This program will allow you to input a circle's raidus" << endl;
cout << "------------------------------------------------------" << endl;
cout << "---and will then output the circumference and area.---" << endl;
cout << "------------------------------------------------------" << endl;
cout << "---------------------Let's begin!---------------------" << endl;
cout << "======================================================" << endl;
cout << endl; cout << endl;
}
void getValue(double& R1){
cout << "Please input the radius of your circle: ";
cin >> R1;
}
double computeCircumference(double R2){
double C1;
double pi = 3.14;
C1 = (2 * pi * R2);
return C1;
}
void computeArea(double R3, double& A1){
double pi = 3.14;
A1 = (pi * R3 * R3);
}
void outputData(double R4, double C2, double A2){
cout << "======================================================" << endl;
cout << "--------------Information on your circle--------------" << endl;
cout << "------------------------------------------------------" << endl;
cout << "Radius--------: " << R4 << endl;
cout << "Circumference-: " << C2 << endl;
cout << "Area----------: " << A2 << endl;
cout << "------------------------------------------------------" << endl;
cout << "-------------Your circle is now complete!-------------" << endl;
cout << "======================================================" << endl;
cout << endl; cout << endl;
}
bool goAgain(){
char run2;
cout << "Do you want to run this program again? (y/n): ";
cin >> run2;
cout << endl; cout << endl;
if ( run2 == 'y' || run2 == 'Y' )
returntrue;
elsereturnfalse;
}
======================================================
--------------Welcome to Project Circle!--------------
------------------------------------------------------
This program will allow you to input a circle's raidus
------------------------------------------------------
---and will then output the circumference and area.---
------------------------------------------------------
---------------------Let's begin!---------------------
======================================================
Please input the radius of your circle: 10
======================================================
--------------Information on your circle--------------
------------------------------------------------------
Radius--------: 10
Circumference-: 62.8
Area----------: 314
------------------------------------------------------
-------------Your circle is now complete!-------------
======================================================
Do you want to run this program again? (y/n): y
Please input the radius of your circle: 5
======================================================
--------------Information on your circle--------------
------------------------------------------------------
Radius--------: 5
Circumference-: 31.4
Area----------: 78.5
------------------------------------------------------
-------------Your circle is now complete!-------------
======================================================
Do you want to run this program again? (y/n): y
Please input the radius of your circle: 3.14
======================================================
--------------Information on your circle--------------
------------------------------------------------------
Radius--------: 3.14
Circumference-: 19.7192
Area----------: 30.9591
------------------------------------------------------
-------------Your circle is now complete!-------------
======================================================
Do you want to run this program again? (y/n):
The modifications (&) were on lines 6, 8, 41 and 58
This version:
01) Declare/Define radius.
02) Call function getValue, send radius to it.
03) Function getValue modified radius to a new value.
04) _main displays the radius as the user's input.
but.. it doesn't work.
It displays my radius as 0, no matter what I input.
That is my problem.
@ cire: I see that your way works, but I don't understand it yet.
Seeing as we haven't touched on ampersands yet, I suppose I'll do some research before implementing the code you provided. I don't like using things I don't understand.
On a side note.. I still don't understand why my code didn't work. Bleh. Research time.
#include "stdafx.h"
#include <iostream>
usingnamespace std;
void getValue(double);
int main(){
double radius = 0.0;
getValue(radius);
cout << "Your radius is: " << radius << endl;
return 0;
}
void getValue(double R1){
cout << "Please input the radius of your circle: ";
cin >> R1;
}
While I don't understand (&) yet, I'm curious as to why the 0.0 isn't getting passed, replaced, and sent back in my 'non working' version. Isn't that what is supposed to happen when I use this code?
Isn't that what is supposed to happen when I use this code?
No. To quote an earlier post in this thread:
cire wrote:
In C++, the default way of passing arguments is by value, so when you call
1 2 3 4 5 6
void getValue(int R1){
cout << "Please input the radius of your circle: ";
cin >> R1;
}
The function gets a copy of the value you fed to it. Then it modifies the copy. Then it returns and the copy it manipulated disappears silently into the night.
By default functions get the value held by the variable that is fed to the function, not the variable itself.
Though even if R1 has the double&, as you said, it's still a copy.
If the original is sent back, and the copy discarded, why does it even matter what datatype is used? It's all erased, according to you, cire.
& in this context has nothing to do with pointers or addresses of variables.
The tutorial on pointers will shed no light on this subject (and indeed the tutorial is wrong calling & the reference operator -- it is the "address of" operator as it is portrayed in that tutorial -- there is no reference operator.) As used to declare references, it would be an attribute specifier.
crystalgem wrote:
Though even if R1 has the double&, as you said, it's still a copy.
No, it's not. I believe I said earlier:
cire wrote:
A reference behaves in every way like an alias (a different name) for the variable it refers to.
Which means when you manipulate the reference, you're effectively manipulating the original.
So since that tutorial isn't accurate, could you link me to one that explains it in depth?
Explains what? Pointers or references? There isn't much more to be said about (lvalue) references, they're pretty simple. You should wait to tackle pointers until you understand more of the less complicated C++ minutiae, since your questions so far lead me to believe you don't have the background to grasp it readily.
Are you taking a class that isn't using any sort of text?
The text in our class is so abstract, that is makes things convoluted to the point of illegibility. This website is far more effective in teaching concepts.
All I want to understand, right now, is the usage of the ampersand in situations like int&.
I get that it's a different type of reference.
I get that it's an alias.
But in the same way that I would say:
I get that a is a letter.
I get that 1 is a number.
I need more than that. I need contextual examples, detailed descriptions, and scrupulous analysis. I need to understand the 'why it is' and 'how it is', not just the 'what it is'. Is there a tutorial, anywhere, that explains it properly?