Function Include If: Calculate Circle Area

Hello,

i've been asked to

Write a C++ program that takes in the radius (as in integer) of a circle from user and calculate the area of the circle through a function. If the area is greater than 50, display the circle’s area along with a message that the circle is big else display the circle’s area and the message that the circle is small. Define a function named calculateArea() to take in the radius as its argument and calculate the circle’s area before returning the area as type double.

what i've done is:

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
#include <iostream>
#include <conio.h>
#include <iomanip> // setprecision(), setiosflags()

const float PI= 3.14;

double calculateArea(int radius);

using namespace std; // std::cout, std::endl, std::cin

// calculate area of circle given the radius and display result
double calculateArea(int radius)
{
using namespace std;

double area = PI*radius*radius;
cout << "The area of the circle with radius " << radius << " is: " << area << setprecision(2)<< endl;
return (area);

}

int main()
{
int radius;

cout << "What is the radius of the circle: ";
cin >> radius;

// call the function
calculateArea(radius);

double area;

if(area>50)
		cout<<area<<" is the big circle"<<endl;
	else if (area!=50)
		cout<<area<<" is a small circle"<<endl;

_getch();
}

confusing part:

when i compile the code, it is work unfortunately when it come to if statement, it say that 'area is not initialized' i already put 'double area' and put it and replace before, after the function but still it says the same error

+ how cn i make if area is not greater than 50?? does it looks like this :

(area>!=50)

im sure it is weird

Last edited on
+ how cn i make if area is not greater than 50?? does it looks like this :


if area is less than 50, then it is not greater than or equal to 50:

 
if(area < 50)


I didn't really read the rest of your post. Please use code tags:

http://cplusplus.com/articles/firedraco1/
fixed about the code tags. sorry. i want to make it in the beginning after i finish the post but i forgot

thnx 4 ur answer but how about the
double area;

error?? it says that area is not initialized

*wondering + thinking

where is the correct place to move it?
closed account (D80DSL3A)
You need to use the return value from your function to assign a value to area in main().

1
2
3
4
5
6
7
double area = calculateArea(radius);

if(area>50)
	cout<<area<<" is the big circle"<<endl;
else // area is <= 50
	cout<<area<<" is a small circle"<<endl;
means??
Please can you explain more?
the program running success but when input the radius, the answer appear as well but then this error appear:

the variable 'area' is being used without being initialized
closed account (D80DSL3A)
The variable named area in the main() is not being assigned any value. The variable named area in the function calculateArea() is not the same one.

If you replace lines 30 and 32 in the code with double area = calculateArea(radius); then area (in main) will get the value returned by the function and all will be well.
thnx guys..this is the fixed code.

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
#include <iostream>
#include <conio.h>
#include <iomanip>

using namespace std;

const double PI= 3.14;
double calculateArea(int radius);

int main()
{
int radius;

cout << "What is the radius of the circle: ";
cin >> radius;

double area = calculateArea(radius);

if(area>50)
		cout<<"The area is "<<area<<" which the circle is big"<<endl;
	else
		cout<<"The area is "<<area<<" which the circle is small"<<endl;

_getch();
}
double calculateArea(int radius)
{

double area = PI*radius*radius;
return (area);

}


problem solved
guys..how can i convert it to pseodo code??
Topic archived. No new replies allowed.