I am running into an issue with my code. Up top I declared and initialized a variable named "userRadius" later I request the user to input a radius so that it will update the variable and display the "diameter" variable. But when I run the program the user input does not affect the diameter variable and it outputs zero, however if I hard code the the math into the output it works just fine. Any ideas?
int option{};
double userRadius{};
const double pi = 3.14159;
//display
cout << "Please enter 1 for Diameter:\nPlease enter 2 for Circumference:\nPlease enter 3 for Area:\nPlease enter -1 to quit:\n";
cin >> option;
//math
//double diameter = userRadius * 2;
const double circumference = 2 * pi * userRadius;
const double area = userRadius * userRadius * pi;
else if (option == 1) {
//output
cout << "Please enter radius: ";
cin >> userRadius;
//cout << userRadius;
out << "The diameter is: " << userRadius * 2;//OUTPUT
}
I created a variable that would be updated by the user input and display the the userinput multiplied by 2 and expected a number which would be the diameter.
C++ is not Excel. Variables are evaluated at the point they are used. Updating the value of a variable will not update any other variables that already used the old value of the updated variable.
To do what you are attempting, you need to recalculate your
circumferenceandareavariables after updatinguserRadius, eg: