0

In the original C program the output was done as

for(i = 0; i < N; i++)
    printf("%-10.4lf", x[i]);

and it looks like

23.4500   22.4420   65.2300   82.3000   7.0000    104.0900

The C++ edition

for(i = 0; i < N; i++)
    std::cout << x[i];

make output as

23.4522.44265.2382.37104.09

What should be changed in C++ edition to see the same result?

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
petrovich
  • 251
  • 1
  • 6

1 Answers1

2

You need setw and setprecision.

for(i = 0; i < N; i++)
{
     std::cout << std::setprecision(4) << x[i];
     std::cout << std::setw(10);  
}
haccks
  • 104,019
  • 25
  • 176
  • 264
  • setprecision( 4 ) was found in http://stackoverflow.com/questions/11989374/floating-point-format-for-stdostream – petrovich Feb 21 '15 at 13:52
  • but still no answer for alignment – petrovich Feb 21 '15 at 13:53
  • 2
    @petrovich: It seems like you're asking us to look stuff up for you: you might find it easier to just do that part yourself. See: http://en.cppreference.com/w/cpp/io/manip (In particular, once you know about `setw`, your next step could be to Google it, and you'd learn that it's called an io manipulator, and you'd find a list of other io manipulators) – Dietrich Epp Feb 21 '15 at 13:56
  • @Dietrich Epp : thank you, it is useful – petrovich Feb 21 '15 at 13:58
  • 1
    Agreed with @DietrichEpp. Look at `std::left`. – haccks Feb 21 '15 at 13:59