im a bit confused on this double constant. its defined as a constant but then used in the program body as different values? i'm just not understanding it i guess. thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
constdouble pi = 3.14159;
constchar newline = '\n';
int main ()
{
double r=5.0; // radius
double circle;
circle = 2 * pi * r;
cout << circle;
cout << newline;
}
const just means that variable will always equal that value. so pi will always = 3.14... you can still use it in other operationss, but you cant change what it equals
You should probably put a return statement like return 0; at the end before the closing bracket. Okay but now what is going on is that you have two different variables of datatype double. One is pi and the other is r. The reason it looks like it is changing is because the value of const pi is multiplied by the variable r and also the number 2. The product of this multiplication is given to the variable circle. Then you cout the value of circle which gave you your answer. if you also printed out the value of pi you would see that it is still the same.
first, there is no return statement because i just copied the code from the tutorial on this site and it didnt have one. my confusion was that i was overthinking it and not seeing double as a datatype but as the constant itself, now i understand! thank you