Start New Discussion within our Software Development Community

I decided to learn the template any how....
but... Each level is giving me the trauma staring up with the domino one after another.. please help....

Problem is
I use both printf(); and cout<< for the console output...
now the problem lies that i can't just leave one of them.. since they both have the amazing distinct features...
for eg.
"cout" -> I don't have to think what i am printing (char, int, float , double)
"printf()" -> I have the facility of some thing like %2d (making spaces)

Piece of Mind: features might be more but my i might be unaware of it... (if possible help me out)

Now problem is.... when i use the templates.
the print() becomes mad.... when the data type goes to hell..
and cout is the thing i know which does not provide me like (%2f) what should i do??

My guess is that you have a template that cannot be expressed as a float/double type
e.g.

template<typename T>
void printout(const T& Obj)
{
  print("%2f",Obj);
}

and then

class SomethingComplex
{
....
};
SomethingComplex A;
printout(A);

There are many possible solutions but why not use the printf() in the class write function
and access by an operator<< overload. e.g.

class S
{
  ...
  public:
     void write(std::ostream&) const;  
};

ostream& operator<<(std::ostream& OX, const S& A)
{
   A.write(OX);
   return OX;
}

void
S::write(std::ostream OX) const
{
  char ss[6];
   sprintf(ss,"%2f %2f\n",internalA,internalB);
   OX<<ss;
}

ALTERNATIVE:

you might want to use the boost library (format).
e.g.

std::cout<<boost::format("%1$-2f") % Number;

BUT this doesn't get round the requirement that Number and the format type need to agree.

This question has already been answered. Start a new discussion instead.