cout or printf
When you output data to the screen do you recommend using cout or printf, I must admit when I first starting using C++ last week I really liked cout. Now I like printf better, It seems easer to format the output like you need it. I am not really sure how to format the output of the cout function. Like in the printf statements below its formatted with 6 places after the decimal point. Can you format the output that way using cout ?
printf("Mag = %12.6f\n",AM[PN]);
printf("GL = %12.6f\n",GL[PN]*180/PI);
cout << "\n\n\n\nPress a key for next Object " ; getch();
guy40az
Junior Poster in Training
57 posts since Mar 2007
Reputation Points: 10
Solved Threads: 2
Yes you can do the same in c++ so use cout in c++. Period.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
How do i get the cout << "GL = " << GL[PN]*180/PI;
to print out to six digits after the decimal point.
guy40az
Junior Poster in Training
57 posts since Mar 2007
Reputation Points: 10
Solved Threads: 2
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
guy40az
Junior Poster in Training
57 posts since Mar 2007
Reputation Points: 10
Solved Threads: 2
Yes you can do the same in c++ so use cout in c++. Period.
Wanna bet?
Ongoing challenge no one has solved yet:Convert this printf() statement into readable C++, output must match exactly:
int iVal = 2;
int hVal= 0x0C;
char *filename = "test.fil";
char *username = "juser";
float fValue = 23.8;
printf("%4d 0x%02X Testfile: [%10s] user: [%-10s]\nTrial %8.2f \n",
iVal, hVal, filename, username, fValue);
Output
2 0x0C Testfile: [ test.fil] user: [juser ]
Trial 23.80
WaltP
Posting Sage w/ dash of thyme
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
Wanna bet?
Ongoing challenge no one has solved yet:
Convert this printf() statement into readable C++, output must match exactly:
int iVal = 2;
int hVal= 0x0C;
char *filename = "test.fil";
char *username = "juser";
float fValue = 23.8;
printf("%4d 0x%02X Testfile: [%10s] user: [%-10s]\nTrial %8.2f \n",
iVal, hVal, filename, username, fValue);
Output
2 0x0C Testfile: [ test.fil] user: [juser ]
Trial 23.80
While I think my code is readable, that certainly doesn't mean it's the best solution to the problem :p
Code:
#include <iostream>
#include <iomanip>
#include <cstdio>
using namespace std;
int main()
{
int iVal = 2;
int hVal= 0x0C;
char *filename = "test.fil";
char *username = "juser";
float fValue = 23.8;
printf("%4d 0x%02X Testfile: [%10s] user: [%-10s]\nTrial %8.2f \n",
iVal, hVal, filename, username, fValue);
cout << setw(4) << iVal;
cout << " 0x" << hex << uppercase << setprecision(2) << setfill('0') << setw(2) << hVal;
cout << setfill(' ') << dec; // reset things
cout << " Testfile: [" << setw(10) << filename << "]";
cout << " user: [" << setw(10) << left << username << right << "]";
cout << "\n";
cout << "Trial " << setw(8) << fixed << setprecision(2) << fValue;
cout << endl;
return 0;
}
Output:
$ g++ blah.cpp && ./a.out
2 0x0C Testfile: [ test.fil] user: [juser ]
Trial 23.80
2 0x0C Testfile: [ test.fil] user: [juser ]
Trial 23.80
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
Finally, someone actually got the output. Good job. I still think printf() is much easier to follow, though.
I like the fact that you put each value in it's own cout statement. That does help.
WaltP
Posting Sage w/ dash of thyme
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
haha, thanks. I was just fiddling around because you said nobody'd done it before. I much prefer the printf as well.
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
$ g++ blah.cpp && ./a.out
2 0x0C Testfile: [ test.fil] user: [juser ]
Trial 23.80
2 0x0C Testfile: [ test.fil] user: [juser ]
Trial 23.80
Nice one.. :)
I also prefer printf() when it comes to formatting..
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
Wanna bet?
Ongoing challenge no one has solved yet:
Maybe you should stop giving your little sister your problems to solve. :)
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Maybe you should stop giving your little sister your problems to solve.
:mrgreen:
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733