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();

How do i get the cout << "GL = " << GL[PN]*180/PI;
to print out to six digits after the decimal point.

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
Comments
Heh. --joeprogrammer

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
Comments
Nice solution :) -joeprogrammer

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.

haha, thanks. I was just fiddling around because you said nobody'd done it before. I much prefer the printf as well.

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

Wanna bet?

Ongoing challenge no one has solved yet:

Maybe you should stop giving your little sister your problems to solve. :)

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