C ++ print spaces or user defined tabs integer

I need to include user input (number) in the output of the TAB spaces. Example I ask the user:

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

(n) I need to turn it into output, for example:

cout << n , endl; 

and what prints on the screen will be the gaps former input 5 out the | _ | <~~~ there are five spaces.

Sorry if I can’t be clear, perhaps for this reason I couldn’t find the answer by looking at other people's questions.

Any help would be greatly appreciated.

+5
source share
4 answers
cout << "Enter amount of spaces you would like (integer)" << endl; 
cin >> n;
//print n spaces
for (int i = 0; i < n; ++i)
{
   cout << " " ;
}
cout <<endl;
+8
source

Just use std::string:

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

, , , , n std::setw.

+27

You just need a loop that repeats the number of times given nand prints a space each time. This would do:

while (n--) {
  std::cout << ' ';
}
+5
source

I was just looking for something similar and came up with this:

std::cout << std::setfill(' ') << std::setw(n) << ' ';
+1
source

All Articles