At 6/30/09 02:43 PM, kiwi-kiwi wrote:
At 6/30/09 01:09 PM, malganis85 wrote:
kiwi-kiwi I think he is talking about c++ you posted c.
Anyway, like wonderful said, C code works in C++, so if a quick solution is available in C, why not use it?
That's *mostly* true but when dealing with C console output functions things tend to get funky. Things work just fine if you only use C++ iostreams or if you only use the C output functions, but as soon as you start mixing them things can get out of sync quickly. You can end up with a situation where you do a
printf("Hello");
cout << "world";
printf("/n");
and your output is "worldHello"
You can mix them, but you have to make sure you call fflush() and cout.flush() every time you switch back and forth. It can be a headache and it's generally better to pick one set of functions and stick with it.
To answer the original question, to do any kind of formatting in C++, you want to use the functions in the iomanip header. If you want to output 5 spaces, you would do this:
#include <iostream>
#include <iomanip>
[snip]
cout << setw(5) << ' ';
If you want to display 20 asterisks, you would do this:
#include <iostream>
#include <iomanip>
[snip]
cout << setfill('*") << setw(5) << ' ';