Connecting Tech Pros Worldwide Help | Site Map

printout of characters

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 11:31 PM
Geoff Jones
Guest
 
Posts: n/a
Default printout of characters

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



  #2  
Old July 22nd, 2005, 11:31 PM
Mike Wahler
Guest
 
Posts: n/a
Default Re: printout of characters


"Geoff Jones" <nodamnspam@email.com> wrote in message
news:41e012e0$0$11839$cc9e4d1f@news.dial.pipex.com ...[color=blue]
> Hiya
>
> Can anybody remind me of the format command,[/color]

C++ does not have 'commands'.
[color=blue]
>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?[/color]

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


  #3  
Old July 22nd, 2005, 11:31 PM
Jerry Coffin
Guest
 
Posts: n/a
Default Re: printout of characters

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

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

std::fill_n(std::ostream_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.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.