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: Textarea max rows and max characters per row - comp.lang ...... values in the same field in consecutive rows ... desktop ... came from not hitting the same character twice in a row. ... ... with zero-terminators, you can print > each ... Printing a 2d char array - comp.lang.c++.moderatedI am trying to print a 2d character array ... columns from a character array. The ... in I/O > Read the fine print on run time formats. An array of ... or multiple rows and ... printing a JLabel text above a JTable - comp.lang.java.gui ...Since the JLabel text is quite long ( 1 or 2 rows) the JTable print method with the two MessageFormat gives me the header in a too big character size and so the text ... How to transpose selected rows into columns and sort by one column ...... Best preparation time (ms) = 1 Rows deleted ... 10s%10s", E, C, $NF) } END { for (; i > 0; i--) print ... your key fields values won't be more than 20 characters. Report Generator - comp.lang.awkThere were no fancy fonts or graphics, just 66 rows of 132 characters. The report ... generator; Author: Johan Rosengren; Updated: 11 Oct 2006; Section: Printing ... Limiting number of characters in a JTable columns? - comp.lang ...Limiting number of characters in a JTable columns? - comp.lang ... count rows - comp.lang.awk I ... PROC REPORT or PROC PRINT with variables > 256 characters - comp ... run ... Print Table of values - comp.soft-sys.math.scilabI have adiscrete function and I can print ... len(x) // find the number of rows in this ... Print a table of ASCII values for the characters in the ASCII character set from 33 ... How to count the occurence of a character in a word - comp.lang ...{ temp = A count = gsub(/;/, "", temp) print count } DKM ... How to count the occurence of a character in a word ... ... count number of rows in a file - comp.soft-sys.matlab ... how to transpose large matrix? - comp.unix.shellYou can read and print the columns as rows one by one. PointedEars ... fun!) one could do, say # assuming each element is 2 characters wide, 7 rows ... matrix / arrays with row, column labels + alignment? - comp.text ...I have a matrix of +- numbers that I want to print ... Is there a package that lets me label rows/cols, and ... dimension" Mark, If your label data is text in c character ... PRINT, LOCATE, then PRINT Misaligned If TEXTSIZE ChangedAfter you CALL TEXTSIZE to change the output text size, rows of characters that you PRINT on the screen still align on the baseline (pixel row) of the ... Printing in rows, then columnsI'm in the final stages of a project and I have a question. Recape + New info - keyword has any non unique characters removed. "Alphabet (without J, so as to keep it ... 7/10/2012 12:41:44 AM
|