Printing rows of characters

  • Follow


Hi, I'm a newbie and I'm trying to program a histogram that outputs
something like this.



Year         0         1         2         3     x 100 miles

 1           ******************************

 2           ********************

 3           ***********************

             etc.


I've got the rest of the program down. But I can't figure out how to
get C++ to print out a row of '*' based on a int value.

I've tried:-

#include<iostream>
using namespace std;

int main()
{
   cout << "*" * 10 << endl;

   return 0;
}

which "works" in Python.

Anybody got any ideas?

Thanks


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply paul62 (1) 4/10/2005 7:17:12 PM

"Paul" <paul@issottp.fsnet.co.uk> writes:

> Year         0         1         2         3     x 100 miles
>
>  1           ******************************
>
>  2           ********************
>
>  3           ***********************
>
>              etc.
>
>
> I've got the rest of the program down. But I can't figure out how to
> get C++ to print out a row of '*' based on a int value.
>
> I've tried:-
>
> #include<iostream>

Side note, unrelated to your question: you also need

#include <ostream>


> using namespace std;
>
> int main()
> {
>    cout << "*" * 10 << endl;

This

std::cout << '*';

writes one asterisk. Wrap it up in a for loop that loops n times, and
you'll have n asterisks.


Or create a std::string object of n asterisks, as in

std::string nAsterisks(n,'*');

and write that.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Thomas 4/10/2005 10:38:24 PM


Paul wrote:
> Hi, I'm a newbie and I'm trying to program a histogram that outputs
> something like this.
>
>
>
> Year         0         1         2         3     x 100 miles
>
>  1           ******************************
>
>  2           ********************
>
>  3           ***********************
>
>              etc.
>
>
> I've got the rest of the program down. But I can't figure out how to
> get C++ to print out a row of '*' based on a int value.

There are several ways -- one is to create and print out a string:

std::cout << std::string(10, '*');

Another is to set the padding for the stream to the desired character,
and then print out one at the right spot:

std::ostream &show_bar(std::ostream &os, unsigned width) {
    char old_fill = os.fill('*');
    os << std::setw(width) << '*';
    os.fill(old_fill);
    return os;
}

yet another is to treat the stream as a collection, and apply an
algorithm to it:

std::fill_n(std::ostream_iterator<char>(std::cout), 10, '*');

--
    Later,
    Jerry.

The universe is a figment of its own imagination.


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply jcoffin 4/10/2005 10:38:47 PM

for loop?

for(int i=0; i<NUM_REQUIRED; i++)
   cout << "*";

cout << endl;



Paul wrote:
> Hi, I'm a newbie and I'm trying to program a histogram that outputs
> something like this.
> 
> 
> 
> Year         0         1         2         3     x 100 miles
> 
>  1           ******************************
> 
>  2           ********************
> 
>  3           ***********************
> 
>              etc.
> 
> 
> I've got the rest of the program down. But I can't figure out how to
> get C++ to print out a row of '*' based on a int value.
> 
> I've tried:-
> 
> #include<iostream>
> using namespace std;
> 
> int main()
> {
>    cout << "*" * 10 << endl;
> 
>    return 0;
> }
> 
> which "works" in Python.
> 
> Anybody got any ideas?
> 
> Thanks

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply l3 4/10/2005 10:40:23 PM

jcoffin@taeus.com wrote:
> Paul wrote:
> > Hi, I'm a newbie and I'm trying to program a histogram that
> > outputs something like this.

> > Year         0         1         2         3     x 100 miles

> >  1           ******************************

> >  2           ********************

> >  3           ***********************

> >              etc.

> > I've got the rest of the program down. But I can't figure
> > out how to get C++ to print out a row of '*' based on a int
> > value.

> There are several ways -- one is to create and print out a string:

> std::cout << std::string(10, '*');

This is doubtlessly what I'd use today, but in the old (C) days,
I used to use a simple trick to output the correct length from a
C style string constant.  Basically:

    char const line[] = "*** ... ***" ;
                // At least as long as the longest string needed.
    char const* const pEnd = line + sizeof( line ) - 1 ;
                // -1 to not count the final '\0'

    ...
    std::cout << pEnd - n ;

(Of course, back in the C days, I'd use printf/fprintf, and a
format specifier of ("%.*s", n, line) would also do the trick.)

--
James Kanze                                           GABI Software
Conseils en informatique orient�e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply kanze 4/12/2005 8:51:56 PM

<kanze@gabi-soft.fr> wrote in message
news:1113300065.458308.95540@z14g2000cwz.googlegroups.com...
> jcoffin@taeus.com wrote:
> > Paul wrote:
> > > Hi, I'm a newbie and I'm trying to program a histogram that
> > > outputs something like this.
>
> > > Year         0         1         2         3     x 100 miles
>
> > >  1           ******************************
>
> > >  2           ********************
>
> > >  3           ***********************
>
> > >              etc.
>
> > > I've got the rest of the program down. But I can't figure
> > > out how to get C++ to print out a row of '*' based on a int
> > > value.

I didn't see your original post, so I'm adding on to Kanze.
You might look into setting the fill character (setfill) and using the setw
manipulator to tell how many fill characters to display. You will need to
include <iomanip> of course. Just output a null string.
-- 
Gary



      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Gary 4/13/2005 6:50:25 AM

jcoffin@taeus.com wrote:
> There are several ways -- one is to create and print out a string:
>
> std::cout << std::string(10, '*');
>
> Another is to set the padding for the stream to the desired character,
> and then print out one at the right spot:
>
> std::ostream &show_bar(std::ostream &os, unsigned width) {
>     char old_fill = os.fill('*');
>     os << std::setw(width) << '*';
>     os.fill(old_fill);
>     return os;
> }
>
> yet another is to treat the stream as a collection, and apply an
> algorithm to it:
>
> std::fill_n(std::ostream_iterator<char>(std::cout), 10, '*');

I thought the first method of creating a std::string object would incur
too much overhead of object creation and dynamic allocation, and that the
third method looked so cool...

A simple benchmark shows that the first outperforms the third.

us      operation
------------------------------------------------------------------------
8.00    for (int i = 0; i < len; ++i) os << '*';
5.70    os << std::string(len, '*');
5.52    char of = os.fill('*'); os << std::setw(len) << ""; os.fill(of);
7.94    std::fill_n(std::ostream_iterator<char>(os), len, '*');
7.88    std::fill_n(osi, len, '*');
        with "std::ostream_iterator<char> osi(os);" out of the loop
[ Tested on Linux 2.6.8 with Intel(R) Xeon(TM) CPU 2.40 GHz ]

Using std::setw was the winner, though it looked a little clumsy to have
to save the old fill value and restore it later.

-- 
Seungbeom Kim

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Seungbeom 4/14/2005 7:06:00 AM

6 Replies
96 Views

(page loaded in 0.11 seconds)

Similiar Articles:













7/10/2012 12:41:44 AM


Reply: