aligning inputed text when it is outputted?What is a smart pointer and when should I use one?When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?When to use virtual destructors?Need help with getline()in c++, is it possible to / how can i display console output after input, on same line, before user's input is given?C++, User input of letters, numbers, and spaces into a stringC++, How cin reads from input buffer?mixing cin and getline input issuesAsk user to enter Lines of text.?Outputting text above user input with cout in separate thread
Help me, I hate squares!
IBM mainframe classic executable file formats
For attacks with ranged weapons, is the attacker's Dexterity bonus added to the damage roll?
Why do MS SQL Server SEQUENCEs not have an ORDER parameter like Oracle?
Why are sugars in whole fruits not digested the same way sugars in juice are?
How to structure presentation to avoid getting questions that will be answered later in the presentation?
Is the EU really banning "toxic propellants" in 2020? How is that going to work?
May a hotel provide accommodation for fewer people than booked?
Applying for mortgage when living together but only one will be on the mortgage
Get content of CMS Block in Magento 1.x
Password management for kids - what's a good way to start?
Are some indefinite integrals impossible to compute or just don't exist?
How to get Planck length in meters to 6 decimal places
If the Moon were impacted by a suitably sized meteor, how long would it take to impact the Earth?
How do discovery writers hibernate?
How to crop this photo of water drops on a leaf to improve the composition?
Russian pronunciation of /etc (a directory)
Is it really a problem to declare that a visitor to the UK is my "girlfriend", in terms of her successfully getting a Standard Visitor visa?
How to prevent a single-element caster from being useless against immune foes?
Is there a way to find the specific variable coefficient in a binomial expansion?
Constant Scan spooling
Solve the mystery of the poisonous chemical
Why should I use a big powerstone instead of smaller ones?
Novel - Accidental exploration ship, broadcasts a TV show to let people know what they find
aligning inputed text when it is outputted?
What is a smart pointer and when should I use one?When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?When to use virtual destructors?Need help with getline()in c++, is it possible to / how can i display console output after input, on same line, before user's input is given?C++, User input of letters, numbers, and spaces into a stringC++, How cin reads from input buffer?mixing cin and getline input issuesAsk user to enter Lines of text.?Outputting text above user input with cout in separate thread
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm working on a program that when the user enters text, they can specify how to align the output. So the user would enter there text then they would be asked what alignment and width they would want for the text (center, left, right, then the width). How would you get the code for the width and alignment? So far I only have the code that gets the users input, but I'm not sure how to get the program to have the user enter their criteria (left,right,center and width)and then align the input given by the user. Here is what I've got so far.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
vector<string> text;
string line;
cout << "Enter Your Text:(to start a newline click enter. When done click enter 2 times " << endl;
while (getline(cin, line) && !line.empty())
text.push_back(line);
cout << "You entered: " << endl;
for (auto &s : text)
cout << s << endl;
cout << "Enter Left,Center,Right and Width: ";
return 0;
I thought maybe I have to use <iomanip>? But I feel like there is another way. Input would be something like.
Hello My Name is Willi
John Doe
and I live in
Kansas.
And then when the user enters the alignments, the text would align so say the user enters right alignment, width 10. The output should be the should be aligned to the right (like in a word processor) and it should have a width of 10 spaces (which I'm assuming would be whitespaces).
c++ iostream
add a comment |
I'm working on a program that when the user enters text, they can specify how to align the output. So the user would enter there text then they would be asked what alignment and width they would want for the text (center, left, right, then the width). How would you get the code for the width and alignment? So far I only have the code that gets the users input, but I'm not sure how to get the program to have the user enter their criteria (left,right,center and width)and then align the input given by the user. Here is what I've got so far.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
vector<string> text;
string line;
cout << "Enter Your Text:(to start a newline click enter. When done click enter 2 times " << endl;
while (getline(cin, line) && !line.empty())
text.push_back(line);
cout << "You entered: " << endl;
for (auto &s : text)
cout << s << endl;
cout << "Enter Left,Center,Right and Width: ";
return 0;
I thought maybe I have to use <iomanip>? But I feel like there is another way. Input would be something like.
Hello My Name is Willi
John Doe
and I live in
Kansas.
And then when the user enters the alignments, the text would align so say the user enters right alignment, width 10. The output should be the should be aligned to the right (like in a word processor) and it should have a width of 10 spaces (which I'm assuming would be whitespaces).
c++ iostream
can you write an example of your input commands as you write them on your terminal and the expected output?
– Yiannis Mpourkelis
Mar 26 at 22:31
@YiannisMpourkelis I edited what the input would be, and how the expected output should be like.
– raj kod
Mar 26 at 22:52
When the user enters Left and width=10, or Center and width=10 or Right and width=10 what is the expected output for your input example?
– Yiannis Mpourkelis
Mar 26 at 23:06
You would have to have a terminal that supports moving the cursor around. And for that,iomanipis not enough. You need thencursesorconio.h(or whatever they use on windows). For starters, you can refresh current line by using'rto move the cursor to the beginning of the line. And for most applications that may be enough.
– Kamil Cuk
Mar 26 at 23:13
add a comment |
I'm working on a program that when the user enters text, they can specify how to align the output. So the user would enter there text then they would be asked what alignment and width they would want for the text (center, left, right, then the width). How would you get the code for the width and alignment? So far I only have the code that gets the users input, but I'm not sure how to get the program to have the user enter their criteria (left,right,center and width)and then align the input given by the user. Here is what I've got so far.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
vector<string> text;
string line;
cout << "Enter Your Text:(to start a newline click enter. When done click enter 2 times " << endl;
while (getline(cin, line) && !line.empty())
text.push_back(line);
cout << "You entered: " << endl;
for (auto &s : text)
cout << s << endl;
cout << "Enter Left,Center,Right and Width: ";
return 0;
I thought maybe I have to use <iomanip>? But I feel like there is another way. Input would be something like.
Hello My Name is Willi
John Doe
and I live in
Kansas.
And then when the user enters the alignments, the text would align so say the user enters right alignment, width 10. The output should be the should be aligned to the right (like in a word processor) and it should have a width of 10 spaces (which I'm assuming would be whitespaces).
c++ iostream
I'm working on a program that when the user enters text, they can specify how to align the output. So the user would enter there text then they would be asked what alignment and width they would want for the text (center, left, right, then the width). How would you get the code for the width and alignment? So far I only have the code that gets the users input, but I'm not sure how to get the program to have the user enter their criteria (left,right,center and width)and then align the input given by the user. Here is what I've got so far.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
vector<string> text;
string line;
cout << "Enter Your Text:(to start a newline click enter. When done click enter 2 times " << endl;
while (getline(cin, line) && !line.empty())
text.push_back(line);
cout << "You entered: " << endl;
for (auto &s : text)
cout << s << endl;
cout << "Enter Left,Center,Right and Width: ";
return 0;
I thought maybe I have to use <iomanip>? But I feel like there is another way. Input would be something like.
Hello My Name is Willi
John Doe
and I live in
Kansas.
And then when the user enters the alignments, the text would align so say the user enters right alignment, width 10. The output should be the should be aligned to the right (like in a word processor) and it should have a width of 10 spaces (which I'm assuming would be whitespaces).
c++ iostream
c++ iostream
edited Mar 26 at 23:07
ilim
3,5647 gold badges17 silver badges36 bronze badges
3,5647 gold badges17 silver badges36 bronze badges
asked Mar 26 at 21:56
raj kodraj kod
212 bronze badges
212 bronze badges
can you write an example of your input commands as you write them on your terminal and the expected output?
– Yiannis Mpourkelis
Mar 26 at 22:31
@YiannisMpourkelis I edited what the input would be, and how the expected output should be like.
– raj kod
Mar 26 at 22:52
When the user enters Left and width=10, or Center and width=10 or Right and width=10 what is the expected output for your input example?
– Yiannis Mpourkelis
Mar 26 at 23:06
You would have to have a terminal that supports moving the cursor around. And for that,iomanipis not enough. You need thencursesorconio.h(or whatever they use on windows). For starters, you can refresh current line by using'rto move the cursor to the beginning of the line. And for most applications that may be enough.
– Kamil Cuk
Mar 26 at 23:13
add a comment |
can you write an example of your input commands as you write them on your terminal and the expected output?
– Yiannis Mpourkelis
Mar 26 at 22:31
@YiannisMpourkelis I edited what the input would be, and how the expected output should be like.
– raj kod
Mar 26 at 22:52
When the user enters Left and width=10, or Center and width=10 or Right and width=10 what is the expected output for your input example?
– Yiannis Mpourkelis
Mar 26 at 23:06
You would have to have a terminal that supports moving the cursor around. And for that,iomanipis not enough. You need thencursesorconio.h(or whatever they use on windows). For starters, you can refresh current line by using'rto move the cursor to the beginning of the line. And for most applications that may be enough.
– Kamil Cuk
Mar 26 at 23:13
can you write an example of your input commands as you write them on your terminal and the expected output?
– Yiannis Mpourkelis
Mar 26 at 22:31
can you write an example of your input commands as you write them on your terminal and the expected output?
– Yiannis Mpourkelis
Mar 26 at 22:31
@YiannisMpourkelis I edited what the input would be, and how the expected output should be like.
– raj kod
Mar 26 at 22:52
@YiannisMpourkelis I edited what the input would be, and how the expected output should be like.
– raj kod
Mar 26 at 22:52
When the user enters Left and width=10, or Center and width=10 or Right and width=10 what is the expected output for your input example?
– Yiannis Mpourkelis
Mar 26 at 23:06
When the user enters Left and width=10, or Center and width=10 or Right and width=10 what is the expected output for your input example?
– Yiannis Mpourkelis
Mar 26 at 23:06
You would have to have a terminal that supports moving the cursor around. And for that,
iomanip is not enough. You need the ncurses or conio.h(or whatever they use on windows). For starters, you can refresh current line by using 'r to move the cursor to the beginning of the line. And for most applications that may be enough.– Kamil Cuk
Mar 26 at 23:13
You would have to have a terminal that supports moving the cursor around. And for that,
iomanip is not enough. You need the ncurses or conio.h(or whatever they use on windows). For starters, you can refresh current line by using 'r to move the cursor to the beginning of the line. And for most applications that may be enough.– Kamil Cuk
Mar 26 at 23:13
add a comment |
1 Answer
1
active
oldest
votes
Here's a simple approach. It simply fills each line with whitespaces based on the alignment. Left alignment is basically the input text. You should probably also check if the alignment width is >= than the longest line of the input.
#include <iostream>
#include <string>
#include <vector>
int main()
std::vector<std::string> text;
std::vector<std::string> output;
std::string line;
std::string align;
int width;
std::cout << "Enter Your Text (to start a newline click enter. When done click enter 2 times): n";
while ( std::getline(std::cin, line) && !line.empty() )
text.push_back(line);
std::cout << "You entered:nn";
for ( auto &s : text )
std::cout << s << "n";
std::cout << "Enter Left,Center,Right and Width: ";
std::cin >> align;
std::cin >> width;
for ( auto &s : text )
int diff = width - s.size(),
half = diff / 2;
if ( align == "Right" )
output.push_back( std::string(diff, ' ') + s );
else if ( align == "Center" )
output.push_back( std::string(half, ' ') + s + std::string(diff - half, ' ') );
else
output.push_back( s );
std::cout << "Aligned Output: nn";
for ( auto &s : output )
std::cout << s << "n";
return 0;
Test Cases (actual output has whitespaces, not *):
Input ( alignment=Right, width=10 ):
Hello my
name is
John Doe
Output:
**Hello my
***name is
**John Doe
Input ( alignment=Center, width=16 ):
Hello my name
is
John Doe
Output:
*Hello my name**
*******is*******
****John Doe****
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55366767%2faligning-inputed-text-when-it-is-outputted%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here's a simple approach. It simply fills each line with whitespaces based on the alignment. Left alignment is basically the input text. You should probably also check if the alignment width is >= than the longest line of the input.
#include <iostream>
#include <string>
#include <vector>
int main()
std::vector<std::string> text;
std::vector<std::string> output;
std::string line;
std::string align;
int width;
std::cout << "Enter Your Text (to start a newline click enter. When done click enter 2 times): n";
while ( std::getline(std::cin, line) && !line.empty() )
text.push_back(line);
std::cout << "You entered:nn";
for ( auto &s : text )
std::cout << s << "n";
std::cout << "Enter Left,Center,Right and Width: ";
std::cin >> align;
std::cin >> width;
for ( auto &s : text )
int diff = width - s.size(),
half = diff / 2;
if ( align == "Right" )
output.push_back( std::string(diff, ' ') + s );
else if ( align == "Center" )
output.push_back( std::string(half, ' ') + s + std::string(diff - half, ' ') );
else
output.push_back( s );
std::cout << "Aligned Output: nn";
for ( auto &s : output )
std::cout << s << "n";
return 0;
Test Cases (actual output has whitespaces, not *):
Input ( alignment=Right, width=10 ):
Hello my
name is
John Doe
Output:
**Hello my
***name is
**John Doe
Input ( alignment=Center, width=16 ):
Hello my name
is
John Doe
Output:
*Hello my name**
*******is*******
****John Doe****
add a comment |
Here's a simple approach. It simply fills each line with whitespaces based on the alignment. Left alignment is basically the input text. You should probably also check if the alignment width is >= than the longest line of the input.
#include <iostream>
#include <string>
#include <vector>
int main()
std::vector<std::string> text;
std::vector<std::string> output;
std::string line;
std::string align;
int width;
std::cout << "Enter Your Text (to start a newline click enter. When done click enter 2 times): n";
while ( std::getline(std::cin, line) && !line.empty() )
text.push_back(line);
std::cout << "You entered:nn";
for ( auto &s : text )
std::cout << s << "n";
std::cout << "Enter Left,Center,Right and Width: ";
std::cin >> align;
std::cin >> width;
for ( auto &s : text )
int diff = width - s.size(),
half = diff / 2;
if ( align == "Right" )
output.push_back( std::string(diff, ' ') + s );
else if ( align == "Center" )
output.push_back( std::string(half, ' ') + s + std::string(diff - half, ' ') );
else
output.push_back( s );
std::cout << "Aligned Output: nn";
for ( auto &s : output )
std::cout << s << "n";
return 0;
Test Cases (actual output has whitespaces, not *):
Input ( alignment=Right, width=10 ):
Hello my
name is
John Doe
Output:
**Hello my
***name is
**John Doe
Input ( alignment=Center, width=16 ):
Hello my name
is
John Doe
Output:
*Hello my name**
*******is*******
****John Doe****
add a comment |
Here's a simple approach. It simply fills each line with whitespaces based on the alignment. Left alignment is basically the input text. You should probably also check if the alignment width is >= than the longest line of the input.
#include <iostream>
#include <string>
#include <vector>
int main()
std::vector<std::string> text;
std::vector<std::string> output;
std::string line;
std::string align;
int width;
std::cout << "Enter Your Text (to start a newline click enter. When done click enter 2 times): n";
while ( std::getline(std::cin, line) && !line.empty() )
text.push_back(line);
std::cout << "You entered:nn";
for ( auto &s : text )
std::cout << s << "n";
std::cout << "Enter Left,Center,Right and Width: ";
std::cin >> align;
std::cin >> width;
for ( auto &s : text )
int diff = width - s.size(),
half = diff / 2;
if ( align == "Right" )
output.push_back( std::string(diff, ' ') + s );
else if ( align == "Center" )
output.push_back( std::string(half, ' ') + s + std::string(diff - half, ' ') );
else
output.push_back( s );
std::cout << "Aligned Output: nn";
for ( auto &s : output )
std::cout << s << "n";
return 0;
Test Cases (actual output has whitespaces, not *):
Input ( alignment=Right, width=10 ):
Hello my
name is
John Doe
Output:
**Hello my
***name is
**John Doe
Input ( alignment=Center, width=16 ):
Hello my name
is
John Doe
Output:
*Hello my name**
*******is*******
****John Doe****
Here's a simple approach. It simply fills each line with whitespaces based on the alignment. Left alignment is basically the input text. You should probably also check if the alignment width is >= than the longest line of the input.
#include <iostream>
#include <string>
#include <vector>
int main()
std::vector<std::string> text;
std::vector<std::string> output;
std::string line;
std::string align;
int width;
std::cout << "Enter Your Text (to start a newline click enter. When done click enter 2 times): n";
while ( std::getline(std::cin, line) && !line.empty() )
text.push_back(line);
std::cout << "You entered:nn";
for ( auto &s : text )
std::cout << s << "n";
std::cout << "Enter Left,Center,Right and Width: ";
std::cin >> align;
std::cin >> width;
for ( auto &s : text )
int diff = width - s.size(),
half = diff / 2;
if ( align == "Right" )
output.push_back( std::string(diff, ' ') + s );
else if ( align == "Center" )
output.push_back( std::string(half, ' ') + s + std::string(diff - half, ' ') );
else
output.push_back( s );
std::cout << "Aligned Output: nn";
for ( auto &s : output )
std::cout << s << "n";
return 0;
Test Cases (actual output has whitespaces, not *):
Input ( alignment=Right, width=10 ):
Hello my
name is
John Doe
Output:
**Hello my
***name is
**John Doe
Input ( alignment=Center, width=16 ):
Hello my name
is
John Doe
Output:
*Hello my name**
*******is*******
****John Doe****
answered Mar 26 at 23:25
DimChtzDimChtz
2,1951 gold badge8 silver badges26 bronze badges
2,1951 gold badge8 silver badges26 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55366767%2faligning-inputed-text-when-it-is-outputted%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
can you write an example of your input commands as you write them on your terminal and the expected output?
– Yiannis Mpourkelis
Mar 26 at 22:31
@YiannisMpourkelis I edited what the input would be, and how the expected output should be like.
– raj kod
Mar 26 at 22:52
When the user enters Left and width=10, or Center and width=10 or Right and width=10 what is the expected output for your input example?
– Yiannis Mpourkelis
Mar 26 at 23:06
You would have to have a terminal that supports moving the cursor around. And for that,
iomanipis not enough. You need thencursesorconio.h(or whatever they use on windows). For starters, you can refresh current line by using'rto move the cursor to the beginning of the line. And for most applications that may be enough.– Kamil Cuk
Mar 26 at 23:13