lets just say that lenght of string is 10 than put 5 spaces on console after string , but if lenght of string is 5 than put 10 spaces on console some buid in function for this purpose in c++ lets say string s1 , s2 and int num1 , num2 are taken from user lets suppose s1 is maham and num1 is 20 s2 is fatima and num2 is 30 so output should be maham 20 fatima 30
i mean to say that spacing depend on lenght of string such that if lenght of string plus spaces equal to some specific number
the only solution came to my mid is that let num store the total lengh lets say num=15 num2 stores lenght of string printing string than using a loop num-num2 imes which prints space than print our required number or anything but i want a solution in form of function which is build into some header file for c++
You can have, from
iomanip
, the functionstd::setw()
that will set the stream width parameter.By default, the data will be put in the rightmost side. Since you want to pad from the right, i.e. write your string from the leftmost side, you could make use of
std::left
to change the default behaviour to your liking.You could then write the following function (example):
The above function accepts a string to write, a width, and optionally a boolean to choose whether or not you want to add a newline.
Live example