Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ printing spaces or tabs given a user input integer

Tags:

c++

tabs

I need to turn user input (a number) into an output of TAB spaces. Example I ask user:

cout << "Enter amount of spaces you would like (integer)" << endl;
cin >> n;

the (n) i need to turn it into an output like:

cout << n , endl; 

and what prints on the screen would be the spaces ex input is 5 out put |_| <~~~there are five spaces there.

Sorry If I can't be clear, probably this is the reason I haven't been able to find an answer looking through other people's questions.

any help will be greatly appreciated.

like image 852
Slider Avatar asked Apr 08 '13 16:04

Slider


1 Answers

Just use std::string:

std::cout << std::string( n, ' ' );

In many cases, however, depending on what comes next, is may be simpler to just add n to the parameter to an std::setw.

like image 119
James Kanze Avatar answered Oct 16 '22 02:10

James Kanze