+ 5

How to multiply ASCII in C++?

I want the result to be like this... @@@@@ So I so this.... cout << (char(64)) * 5; For some reason the result is printing stuff that I don't want. Anyone have an answer?

3rd Nov 2017, 1:17 PM
DeltaTick
DeltaTick - avatar
6 Réponses
3rd Nov 2017, 1:37 PM
Kirk Schafer
Kirk Schafer - avatar
+ 6
Well, std::string has 9 constructors ( C++11 ) 1 of them is a fill constructor which takes 2 arguments. 1: the size 2: the character So std::string( 5, 64 ) then creates a string object of size 5, filled with @'s std::string( 5, 64 ) = "@@@@@" std::string( 7, '.' ) = "......." int a = 6; char b = '&'; std::string( a, b ) = "&&&&&&"
3rd Nov 2017, 1:35 PM
Dennis
Dennis - avatar
+ 4
You can use the std::string's fill constructor to 'multiply'. std::string( amount to fill, char ); std::cout << std::string( 5, 64 );
3rd Nov 2017, 1:30 PM
Dennis
Dennis - avatar
+ 4
Dennis, can you explain that a little more?
3rd Nov 2017, 1:31 PM
DeltaTick
DeltaTick - avatar
+ 2
you need to create a function for that.
3rd Nov 2017, 1:28 PM
Cain Eviatar
Cain Eviatar - avatar