+ 1

How to print ('0' * 3) as '000' or '9' + '0' = '90' in c++?

1. cout<<'0'*3; 2. string s = "0"; cout<< s*3; 3.char c = '0'; cout<< c*3; I tried all above things and got huge errors....

11th May 2020, 1:06 PM
Amit Dubey
Amit Dubey - avatar
2 ответов
+ 2
std::string has no predefined * operator that will multiply a string by an int, but you can define your own: https://code.sololearn.com/cOERAM04A9g7/#cpp but if you want to repeat the character ( char type ) , std::string has a constructor of the form: "std::string(size_type count, char c);" https://code.sololearn.com/c6j2BVRj81Bk/#cpp
11th May 2020, 2:26 PM
MO ELomari
MO ELomari - avatar
+ 1
I am too naive to understand how made your own string operator but i got the second one... Thank you :D Mohamed ELomari
11th May 2020, 2:41 PM
Amit Dubey
Amit Dubey - avatar