I'm working on a program that finds the area and circumference of a circle using the radius input by the user. I'm trying to get the value of the radius to be carried over to the void functions to correctly calculate the area and circumference and for those values to be carried back into the main function to be displayed. Any help would be appreciated.
#include <iostream>
using namespace std;
const double pi =3.14;
void findArea();
void findCircumference();
int main()
{
int radius ,area, circumference;
cout << "Enter circle's radius:" << endl;
cin >> radius;
findArea ();
findCircumference();
cout << "Area is " << area << endl;
cout << "Circumference is " << circumference << endl;
return 0;
}
void findArea()
{
int radius, area;
area = radius*radius*pi;
}
void findCircumference()
{
int radius, circumference;
circumference = 2*radius*pi;
}
#include <iostream>
using std::cin;
using std::cout;
int findArea(int r); // As you are dealing with numbers, use one!
int findCircumferance(int r); // EDIT: Missing colon
/*
data type : function(data type input)
int boxArea(int length, int width)
{
return(length * width); // cout << boxArea(length, width); == cout << length * width;
}
*/
constdouble pi = 3.14;
int area, circumference, radius; // Global variables, all functions can use these
int main()
{
cout << "Enter circle's radius:" << endl;
cin >> radius;
//findArea (); No need for these anymore, calculates when used
//findCircumference();
cout << "Area is " << findArea(radius) << endl;
cout << "Circumference is " << findCircumference(radius) << endl;
return 0;
}
int findArea(int r)
{
area = (r * r) * pi;
return area;
}
int findCircumference(int r)
{
circumference = 2 * r * pi;
return circumference;
}
Do. Not. Use. Global. Variables. Aside from global constant, and in that case it is better to mark them constexpr. Global variables is basically a gun with loose trigger aimed at you foot. So you must not use them, until you will understand all implications of them and can bear responsibility for it.
Do. Not. Use. Global. Variables. Aside from global constant, and in that case it is better to mark them constexpr. Global variables is basically a gun with loose trigger aimed at you foot. So you must not use them, until you will understand all implications of them and can bear responsibility for it.
I understand this, I assumed this is a beginner project. He is trying to understand how to pass variables to functions, not program design. One step at a time.
Bad practice is one thing, experimenting and understanding what one is doing while learning the basics of a computer language is another, please treat it as such.
1 2 3 4 5 6 7 8 9 10
// Take this line of code
int area, circumference, radius;
// and put it here
int main()
{
int area, circumference, radius;
cout << "Enter circle's radius:" << endl;
cin >> radius;
It is criminal to force upon someone something which will have to be removed later. Do not claim that global variables are fine for beginners - habits are hard to break.
I did not force any method unto him/her. I did not claim that global variables are good practice, at all.
The example I did was off the top of my head, he wanted help. Sure, I should of just put the variables inside main but I didn't. Gee, I am sorry for helping someone when no one else could be bothered to.
If you read their code, you will know they won't use global variables, he put them in main. The only thing he would of used to solve his question is using int's to return values rather than a void. Which was the basis of the question.
TL;DR - I will go now before this thread derails anymore, the question is solved.