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.
#include <iostream>
#include <conio.h>
#include <iomanip> // setprecision(), setiosflags()
constfloat PI= 3.14;
double calculateArea(int radius);
usingnamespace std; // std::cout, std::endl, std::cin
// calculate area of circle given the radius and display result
double calculateArea(int radius)
{
usingnamespace 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;
elseif (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 :
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.
#include <iostream>
#include <conio.h>
#include <iomanip>
usingnamespace std;
constdouble 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);
}