0

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

6
Contributors
11
Replies
15
Views
9 Years
Discussion Span
Last Post by ~s.o.s~
Featured Replies
  • 1
    WaltP 2,905   9 Years Ago

    [QUOTE=iamthwee;337218]Yes you can do the same in c++ so use cout in c++. Period.[/QUOTE] Wanna bet? Ongoing challenge no one has solved yet: [i][color=blue]Convert this printf() statement into [u]readable[/u] C++, output must match exactly:[/color][/i] [code] int iVal = 2; int hVal= 0x0C; char *filename = "test.fil"; char *username = "juser"; … Read More

  • [quote=WaltP;337404]Wanna bet? Ongoing challenge no one has solved yet: [I][COLOR=blue]Convert this printf() statement into [U]readable[/U] C++, output must match exactly:[/COLOR][/I] [code] 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, … Read More

0

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

1

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
1

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
0

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.

0

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

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

0

Wanna bet?

Ongoing challenge no one has solved yet:

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.