I need help with this problem. I have tried writting the program, i just can not get it to run properly.

Problem is:

Create a function named 'circle_area' that will find the area of a circle. This function should have the following prototype:

float circle_area(float radius)

circle_area should return PI*r^2. If passed 3.25 it will return 33.18. If passed 5.0 it should return 78.53, etc.

The main function will call circle_area and pass it a float that you stored in a variable. The value of Pi should be stored in a constant. The main program should display the variable and the area. For example, if radius is 1.0, the main function should display 'The area of a circle with radius 1.0 is: 3.14'. Use the techniques in section 3.7 of the textbook to format your output to 2 decimal places.

Attached is what i have.

Edited 4 Years Ago by WaltP: zombie magnet

Attachments
#include <iostream>;
#include <fstream>;
#include <iomanip>;
#include <string>;
#include <cmath>;
using namespace std;



	

	float PI= 3.14;

float main()
{
	
	float radius;

	cout<< "What is the radius of the circle?"<<endl;
	cin>> radius;
	return 0;
}	

	
	
float circle_area(float radius)
{
	
	float area= PI*radius*radius;

	cout<< "The area of the circle with radius "<<radius<< "is:"<< area<<setprecision(2)<<endl;

	return (area);
}

Man you've done most of the work, just call your function in main, here is the code with some changes:

#include <iostream>
#include <iomanip>

const float PI= 3.14;

float circle_area(float &radius);

float circle_area(float &radius)
{
	using namespace std;

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

int main()
{
	using namespace std;
	
	float radius;
	cout<< "What is the radius of the circle:> ";
	cin >> radius;
	circle_area(radius);
	return 0;
}

Intel

As your code is so small, you should post it betwen code tags.
Some errors I noticed:
- at the end of include directives you do not have to put a semicolon(;)
- main allways returns int
- you have to declare the function circle_area before main
- you don't call the function circle_area inside the main, as you should
- you don't need all those headers (iostream and iomanip will suffice)

You need both

#include <iostream>
#include <iomanip>

I/O ops defined in STD namespace .... I/O manipulators are deined in iomanip

>I/O ops defined in STD namespace .... I/O manipulators are deined in iomanip
They're all in the std namespace. The streams and manipulators without arguments are declared in iostream, manipulators that take arguments are declared in iomanip. So to use setw and the like, you need to include iomanip, but to use left or hex and friends, you only need iostream.

You are almost there, but you have to fix your precision and perhaps make the console wait so you can see the result. Thoroughly check the code and learn ...

// program to calculate area of circle   (tested with Dev-C++)

#include <iostream>
#include <iomanip>  // setprecision(), setiosflags()

const float PI= 3.14;

float circle_area(float radius);

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

// calculate area of circle given the radius and display result
float circle_area(float radius)
{
	using namespace std;

	float area = PI*radius*radius;
	cout << setiosflags(ios::fixed) << setprecision(2);
	cout << "The area of the circle with radius " << radius << " is: " << area << endl;
	return (area);
}

int main()
{
	float radius;
	
	cout << "What is the radius of the circle:> ";
	cin >> radius;
	// call the function
	circle_area(radius);
	
	cin.sync();  // purge enter
	cin.get();   // console wait
	return 0;
}

Not an error, but a little goofy, the line
using namespace std;
should be removed from the function, don't know how it snuck in.

Just to make things perfect, your function doesn't need to return anything so it could be rewritten as ...

void circle_area(float radius) 
{ 
    float area = PI*radius*radius; 
    cout << setiosflags(ios::fixed) << setprecision(2); 
    cout << "The area of the circle with radius " << radius << " is: " << area << endl; 
}

Then you also have to change the optional but recommended prototype to: void circle_area(float radius);

#include <iostream>
using namespace std;

int main()
{

Comments
Come back when you can walk without dribbling on your shoes.

Man you've done most of the work, just call your function in main, here is the code with some changes:

#include <iostream>
#include <iomanip>

const float PI= 3.14;

float circle_area(float &radius);

float circle_area(float &radius)
{
	using namespace std;

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

int main()
{
	using namespace std;
	
	float radius;
	cout<< "What is the radius of the circle:> ";
	cin >> radius;
	circle_area(radius);
	return 0;
}

Intel

That will work but a function should really stick to only doin one thing... either calculation or cout, since this is not a complex task.

float circle_area(float radius)
{
	return PI * radius * radius;
}

Then do the cout's in main

That will work but a function should really stick to only doin one thing... either calculation or cout, since this is not a complex task.

float circle_area(float radius)
{
	return PI * radius * radius;
}

Then do the cout's in main

This thread needs no more help. The thread ended in 2005. I'm sure the OP is no longer waiting...

This article has been dead for over six months. Start a new discussion instead.