13

I need to create a string of blanks in c++, where the number of spaces is a variable, so I can't just type it in. How do I do this without looping ?

Thanks!

R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
MLP
  • 735
  • 2
  • 7
  • 7

4 Answers4

24
size_t size = 5; // size_t is similar to unsigned int ‡
std::string blanks(size, ' ');

See: http://www.cplusplus.com/reference/string/string/string/

‡ See the question on size_t if this isn't clear.

Community
  • 1
  • 1
Brendan Long
  • 53,280
  • 21
  • 146
  • 188
8
#include <string>
std::string mystring(5,' ');
cape1232
  • 999
  • 6
  • 21
6
#include <string>
.....
//i is your variable length
string s_blanks_length_i( i, ' ' );
MadMurf
  • 2,247
  • 3
  • 23
  • 30
4

5 spaces to standard output:

std::cout << std::string( 5, ' ' ) << std::endl;
iqmaker
  • 2,162
  • 25
  • 24