printout of characters

G

Geoff Jones

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
 
M

Mike Wahler

Geoff Jones said:
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
 
J

Jerry Coffin

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top