When in doubt, use the old workhorse printf(). I have played around with manipulators like [php]cout << setfill('0') << setw(8) << d1;
[/php] with results that cause only consternation. The old printf() like Chainsaw mentioned works well:
[php]// right justified numeric output (Dev-C++)
#include
#include // may have to include stdio.h with VC++
using namespace std;
int main()
{
double d1 = 30.768;
double d2 = 1.345;
double d3 = .430;
printf("%8.3f\n",d1);
printf("%8.3f\n",d2);
printf("%8.3f\n\n",d3);
system("PAUSE");
return EXIT_SUCCESS;
}
[/php]
vegaseat
DaniWeb's Hypocrite
5,986 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
When challenged, think a little!!!!!!!!!! Yes you can line up your decimal points with std::cout ...
[php]// right justified numeric output (Dev-C++)
#include
#include
#include // setXXX() functions
using namespace std;
int main()
{
double d1 = 30.768;
double d2 = 1.345;
double d3 = .430;
// this works well, the decimal points line up
printf("%8.3f\n",d1);
printf("%8.3f\n",d2);
printf("%8.3f\n\n",d3);
// dito, but keep the order of setXXX() functions!!
cout << setiosflags(ios::right);
cout << setiosflags(ios::fixed);
cout << setw(8) << setprecision(3) << d1 << endl;
cout << setw(8) << setprecision(3) << d2 << endl;
cout << setw(8) << setprecision(3) << d3 << endl << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
[/php]
vegaseat
DaniWeb's Hypocrite
5,986 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
A friendly note to Siersan:
Spend some time with your editor to look at the iostream header. The one that comes with Dev-C++ leads you into a trail of internal includes to other header files which in turn include other header files and so on. Surprise, somewhere near the end is stdio.h so printf() is taken care off! Even cstdio simply includes stdio.h .....
You are right, to keep the notorius complainers of your back, it is better to include the obvious headers from the books! My oversight, I profoundly apologize!
vegaseat
DaniWeb's Hypocrite
5,986 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
>Spend some time with your editor to look at the iostream header.
Spend some time with the C++ standard. iostream isn't required to include cstdio or stdio.h, so your code exhibits undefined behavior.
>The one that comes with Dev-C++
Not everyone uses Dev-C++.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>Spend some time with your editor to look at the iostream header.
Spend some time with the C++ standard. iostream isn't required to include cstdio or stdio.h, so your code exhibits undefined behavior.
>The one that comes with Dev-C++
Not everyone uses Dev-C++.
You are right, to keep the notorius complainers of your back, it is better to include the obvious headers from the books! My oversight, I profoundly apologize!
vegaseat
DaniWeb's Hypocrite
5,986 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417