>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
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
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
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
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
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
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
peter_budo
Code tags enforcer
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902