printout of characters

Discussion in 'C++' started by Geoff Jones, Jan 8, 2005.

  1. Geoff Jones

    Geoff Jones Guest

    Hiya

    Can anybody remind me of the format command, or otherwise, to print a number
    of the same character e.g. if I wanted to printout 20 *'s

    ********************
    then I'm sure there is an inbuilt command, without using a loop, that will
    do it.

    Can anybody remind me of what it is?

    Ta

    Geoff
     
    Geoff Jones, Jan 8, 2005
    #1
    1. Advertising

  2. Geoff Jones

    Mike Wahler Guest

    "Geoff Jones" <> wrote in message
    news:41e012e0$0$11839$...
    > Hiya
    >
    > Can anybody remind me of the format command,


    C++ does not have 'commands'.

    >or otherwise, to print a number
    > of the same character e.g. if I wanted to printout 20 *'s
    >
    > ********************
    > then I'm sure there is an inbuilt command, without using a loop, that will
    > do it.
    >
    > Can anybody remind me of what it is?


    There are a virtually unlimited number of possible ways.
    I'd do it like this:

    #include <iostream>
    #include <string>

    int main()
    {
    const std::string::size_type count(20);
    char c('*');

    std::cout << std::string(count, c) << '\n';
    return 0;
    }

    -Mike
     
    Mike Wahler, Jan 8, 2005
    #2
    1. Advertising

  3. Geoff Jones

    Jerry Coffin Guest

    > Can anybody remind me of the format command, or otherwise, to print a
    number
    > of the same character e.g. if I wanted to printout 20 *'s


    > ********************


    There are quite a few possibilities. One reasonably clean one would be:

    std::fill_n(std::eek:stream_iterator<char>(std::cout), 20, '*');

    If you prefer to use only the native abilities of iostreams, you could
    use:

    std::cout << std::setfill('*') << std::setw(20) << ' ';

    but this prints an extra space at the end of the line -- if that's all
    that's going to go on the line, you might prefer:

    std::cout << std::setfill('*') << std::setw(20) << '\n';

    or, if you don't want a new line, something like:

    std::cout << std::setfill('*') << std::setw(19) << '*';

    At least in theory, the versions using iostreams capabilities might be
    marginally more efficient, but given that this is producing output on a
    stream, it's hard to imagine how it would make any real difference, so
    at least to me, fill_n seems the obvious choice.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Jan 8, 2005
    #3
    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. TJS
    Replies:
    0
    Views:
    295
  2. Mike Grace

    Can I output a printout as a tif?

    Mike Grace, Aug 27, 2003, in forum: Java
    Replies:
    1
    Views:
    363
    Mike Grace
    Aug 27, 2003
  3. Bill Dennis

    Imbedding Word Files in Printout

    Bill Dennis, Dec 5, 2003, in forum: Java
    Replies:
    0
    Views:
    318
    Bill Dennis
    Dec 5, 2003
  4. Replies:
    3
    Views:
    522
    John C. Bollinger
    Mar 8, 2006
  5. Stefan Mueller
    Replies:
    4
    Views:
    1,490
    wayne
    Nov 26, 2005
Loading...

Share This Page