I need help understanding this error.

Pages: 12
It is your problem. The initialization of radius and area don't matter one iota to the output of your program when coded correctly.

& in this context has nothing to do with pointers or addresses of variables.
Your code + 4 ampersands, with output:


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
#include "stdafx.h"
#include <iostream>
using namespace 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' )
      return true;
   else
      return false;

}
======================================================
--------------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
Last edited on
Here is the dumbed down version of my problem.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"
#include <iostream>
using namespace 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;

}
Last edited on
Read the previous post. =P
@ 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.
Because u are passing variables by value.
This works:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"
#include <iostream>
using namespace 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;

}


This does not work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"
#include <iostream>
using namespace 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.
Okay; I think my problem is that I've completely misunderstand how send/receive works for void. Ive been under the assumption that:


this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"
#include <iostream>
using namespace 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;

}



is the same as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "stdafx.h"
#include <iostream>
using namespace std; 
int getValue(int);

int main(){
   int radius = 0.0;
   radius = getValue(radius); 
   cout << "Your radius is: " << radius << endl;

return 0;
}

int getValue(int R1){
   cout << "Please input the radius of your circle: "; 
   cin  >> R1;
   return R1;
}


After reading this:
http://www.cplusplus.com/doc/tutorial/pointers/
, I'm starting to see how you're grabbing the values via address.

I'll read a bit more and hit you guys up tomorrow with a few more questions. That's all for tonight. Thanks guys. :)
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.
Like I said earlier in the thread...

cire wrote:
& 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.
@cire

Which means when you manipulate the reference, you're effectively manipulating the original


Except references to bit fields.:)
So since that tutorial isn't accurate, could you link me to one that explains it in depth?

when you manipulate the reference, you're effectively manipulating the original.
just isn't enough for me to grasp this subject in its entirety.
Last edited on
Okay, so no one has a tutorial.

Anyone have a couple spare hours, a good knowledge of references, and Windows Live Messenger or Skype?
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?
Last edited on
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?
You declared 3.14 as int

 
int pi = 3.14


This is adsolutely wrong. as the pi would be read as
 
pi=3
@wilson90; read the rest of the thread. We've already switched that to Double. :P
Topic archived. No new replies allowed.
Pages: 12