when i set y in my case to 0, i seem to have a gap at the top line of the output like this:

Code:

A
BB
CCC
DDDD

where as i set y to 1, i get:

Code:

A
BB
CCC
DDDD

Whoa!!! Actually i didn't think about the constructing a string.
The result you're getting is normal. Setting y=0 means you're creating a first string with only spaces and the second one is empty.
The string constructor builds up a string with n specified characters. In your case the first string si a 5-y length string of ' ', second one is a 0+y length string of 64+y characters.

Note that I left the 4 since it's the number of characters you want to print, so if you wanto to parameterize the function... (and notice that the 3 in the first string constructor is 4-1 ;))

Regards,
Tesctassa

11-10-2007

CornedBee

Masses of magic numbers there!

At least replace 65 by 'A'.

11-10-2007

SRS

:)

Wow that is nice to know...
I didn't know such a thing was possible:

Code:

cout << string((1+y), 65+y);

This looks like less work on the person's part when writing it, but how would it compare as far as performance to method A listed above?

11-10-2007

CornedBee

It really depends on the std::string implementation. In a typical, modern implementation (small buffer optimization, good inlining by the compiler) I'd say it compares favourably, mostly because iostreams are dog slow, and the less you call into them, the better.
If the string implementation does heap allocation even for 4 characters, the performance balance might tip into the individual character output direction. You'd have to measure it.

11-10-2007

DrSnuggles

Second one is trickier, you can do it with 5 abstractly nested "for a while" loops but that has rarely been tried. Your safest strategy is to do it without thinking.