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

Member Avatar for iamthwee

Yes you can do the same in c++ so use cout in c++. Period.

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

Thanks for the info.

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
commented: Heh. --joeprogrammer +8

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
commented: Nice solution :) -joeprogrammer +9

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

Member Avatar for iamthwee

Wanna bet?

Ongoing challenge no one has solved yet:

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

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

:mrgreen:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.