954,173 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ homework

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.

Attachments Lab3.cpp (0.47KB)
sutty8303
Newbie Poster
4 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

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

Intel
Light Poster
30 posts since Feb 2005
Reputation Points: 10
Solved Threads: 1
 

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)

frrossk
Posting Whiz in Training
220 posts since Sep 2004
Reputation Points: 17
Solved Threads: 9
 

You need both

#include <iostream>
#include <iomanip>

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

Intel
Light Poster
30 posts since Feb 2005
Reputation Points: 10
Solved Threads: 1
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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 ...
[php]
// program to calculate area of circle (tested with Dev-C++)

#include
#include // 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;
}
[/php]

vegaseat
DaniWeb's Hypocrite
Moderator
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
 

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 ...
[php]
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;
}
[/php]
Then you also have to change the optional but recommended prototype to: void circle_area(float radius);

vegaseat
DaniWeb's Hypocrite
Moderator
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
 

#include
using namespace std;

int main()
{

PZY_DAVE
Newbie Poster
1 post since Jan 2010
Reputation Points: 6
Solved Threads: 0
 

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

hag++
Junior Poster
197 posts since Jan 2010
Reputation Points: 34
Solved Threads: 31
 

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...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
 

Thread closed

peter_budo
Code tags enforcer
Moderator
15,433 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 901
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You