Trouble with carrying value of variables

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;
}
1
2
3
4
5
6
7
int findArea(int radius)
{
    return radius * radius * pi;
}

//main()
int area = findArea(radius);


http://www.cplusplus.com/doc/tutorial/functions/
closed account (G30oGNh0)
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
#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;
     }
*/

const double 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;
}
Last edited on
I GuNNeR I wrote:
Global variables, all functions can use these
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.
closed account (G30oGNh0)

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;



You will get the same desired effect.
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.
closed account (G30oGNh0)
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.
Thanks for all your help guys, I appreciate it. It really helped clarify to me how to solve this problem
Topic archived. No new replies allowed.