Can cout do like printf("Name: %s, Age: %d\n",name,age)?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cylin

    Can cout do like printf("Name: %s, Age: %d\n",name,age)?

    Dear all,

    To do below is too long.
    cout << "Name: " << name << ", Age: " << age << endl;
    Can cout use "%" to implement?
    Thanks.

    cylin.



  • Sam Holden

    #2
    Re: Can cout do like printf(&quot;Na me: %s, Age: %d\n&quot;,name ,age)?

    On Tue, 19 Aug 2003 18:54:29 +0800, cylin <cylin@avant.co m.tw> wrote:[color=blue]
    > Dear all,
    >
    > To do below is too long.
    > cout << "Name: " << name << ", Age: " << age << endl;[/color]

    If 20 characters of typing is enough to make you want to avoid
    type safety, then I don't C++ is the language for you. :)
    [color=blue]
    > Can cout use "%" to implement?[/color]

    No.

    --
    Sam Holden

    Comment

    • Peter van Merkerk

      #3
      Re: Can cout do like printf(&quot;Na me: %s, Age: %d\n&quot;,name ,age)?

      > To do below is too long.[color=blue]
      > cout << "Name: " << name << ", Age: " << age << endl;[/color]

      Why it is too long?
      If you would remove the spaces around the '<<' operators (just like the
      printf), the cout version is only 4 character longer. That seems to be a
      small price to pay for the type safety you get with cout.
      [color=blue]
      > Can cout use "%" to implement?[/color]

      No.

      --
      Peter van Merkerk
      peter.van.merke rk(at)dse.nl




      Comment

      • John Harrison

        #4
        Re: Can cout do like printf(&quot;Na me: %s, Age: %d\n&quot;,name ,age)?


        "cylin" <cylin@avant.co m.tw> wrote in message
        news:bhsvii$2rh fn$1@ID-154203.news.uni-berlin.de...[color=blue]
        > Dear all,
        >
        > To do below is too long.
        > cout << "Name: " << name << ", Age: " << age << endl;
        > Can cout use "%" to implement?
        > Thanks.
        >
        > cylin.
        >[/color]

        No, but boost do a format library that adds this to C++



        john


        Comment

        • Bob Jacobs

          #5
          Re: Can cout do like printf(&quot;Na me: %s, Age: %d\n&quot;,name ,age)?

          "cylin" <cylin@avant.co m.tw> wrote in message
          news:bhsvii$2rh fn$1@ID-154203.news.uni-berlin.de...[color=blue]
          > Dear all,
          >
          > To do below is too long.
          > cout << "Name: " << name << ", Age: " << age << endl;
          > Can cout use "%" to implement?
          > Thanks.[/color]

          Your two examples are not equivalent. In the version using cout you flush
          the output stream, whereas in the printf version you don't.


          Comment

          • Roy Smith

            #6
            Re: Can cout do like printf(&quot;Na me: %s, Age: %d\n&quot;,name ,age)?

            In article <bhsvii$2rhfn$1 @ID-154203.news.uni-berlin.de>,
            "cylin" <cylin@avant.co m.tw> wrote:
            [color=blue]
            > Dear all,
            >
            > To do below is too long.
            > cout << "Name: " << name << ", Age: " << age << endl;
            > Can cout use "%" to implement?
            > Thanks.
            >
            > cylin.
            >
            >
            >[/color]

            Check out boost.format (http://www.boost.org/libs/format/index.htm)

            Comment

            • Mike Wahler

              #7
              Re: Can cout do like printf(&quot;Na me: %s, Age: %d\n&quot;,name ,age)?

              cylin <cylin@avant.co m.tw> wrote in message
              news:bhsvii$2rh fn$1@ID-154203.news.uni-berlin.de...[color=blue]
              > Dear all,
              >
              > To do below is too long.
              > cout << "Name: " << name << ", Age: " << age << endl;[/color]

              Removing unnecessary whitepace and using
              same newline expression in order to form
              a valid comparision:

              printf("Name: %s, Age: %d\n",name,age) ;

              cout<<"Name: "<<name<<", Age: "<<age<<'\n ';

              Is an extra four characters really too high
              a price to pay for type safety?
              [color=blue]
              > Can cout use "%" to implement?[/color]

              I suppose one could write some code to simulate
              it, but what's the point?

              Also note that 'printf()' is just as valid
              (but less safe) a function in C++ as are the
              stream inserters.

              If you insist upon using '%' specifiers, why not just
              use 'printf()'?

              -Mike





              Comment

              Working...