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

Discussion in 'C++' started by cylin, Aug 19, 2003.

  1. cylin

    cylin Guest

    Dear all,

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

    cylin.
     
    cylin, Aug 19, 2003
    #1
    1. Advertising

  2. cylin

    Sam Holden Guest

    On Tue, 19 Aug 2003 18:54:29 +0800, cylin <> wrote:
    > Dear all,
    >
    > To do below is too long.
    > cout << "Name: " << name << ", Age: " << age << endl;


    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. :)

    > Can cout use "%" to implement?


    No.

    --
    Sam Holden
     
    Sam Holden, Aug 19, 2003
    #2
    1. Advertising

  3. > To do below is too long.
    > cout << "Name: " << name << ", Age: " << age << endl;


    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.

    > Can cout use "%" to implement?


    No.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
     
    Peter van Merkerk, Aug 19, 2003
    #3
  4. "cylin" <> wrote in message
    news:bhsvii$2rhfn$-berlin.de...
    > Dear all,
    >
    > To do below is too long.
    > cout << "Name: " << name << ", Age: " << age << endl;
    > Can cout use "%" to implement?
    > Thanks.
    >
    > cylin.
    >


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

    http://www.boost.org/libs/format/index.htm

    john
     
    John Harrison, Aug 19, 2003
    #4
  5. cylin

    Bob Jacobs Guest

    "cylin" <> wrote in message
    news:bhsvii$2rhfn$-berlin.de...
    > Dear all,
    >
    > To do below is too long.
    > cout << "Name: " << name << ", Age: " << age << endl;
    > Can cout use "%" to implement?
    > Thanks.


    Your two examples are not equivalent. In the version using cout you flush
    the output stream, whereas in the printf version you don't.
     
    Bob Jacobs, Aug 19, 2003
    #5
  6. cylin

    Roy Smith Guest

    In article <bhsvii$2rhfn$-berlin.de>,
    "cylin" <> wrote:

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


    Check out boost.format (http://www.boost.org/libs/format/index.htm)
     
    Roy Smith, Aug 19, 2003
    #6
  7. cylin

    Mike Wahler Guest

    cylin <> wrote in message
    news:bhsvii$2rhfn$-berlin.de...
    > Dear all,
    >
    > To do below is too long.
    > cout << "Name: " << name << ", Age: " << age << endl;


    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?

    > Can cout use "%" to implement?


    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
     
    Mike Wahler, Aug 19, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    833
    Roedy Green
    Jan 2, 2006
  2. abi

    man cout or info cout

    abi, Jun 27, 2003, in forum: C++
    Replies:
    2
    Views:
    1,653
  3. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,411
    Leor Zolman
    Jun 2, 2004
  4. Replies:
    4
    Views:
    319
    Howard
    Sep 15, 2005
  5. Stefan Ram

    Re: cout vs std::cout

    Stefan Ram, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    481
Loading...

Share This Page