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;








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).










share|improve this question


























  • 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, 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


















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).










share|improve this question


























  • 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, 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














0












0








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).










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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, 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


















  • 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, 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

















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













1 Answer
1






active

oldest

votes


















0














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****






share|improve this answer
























    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
    );



    );













    draft saved

    draft discarded


















    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









    0














    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****






    share|improve this answer





























      0














      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****






      share|improve this answer



























        0












        0








        0







        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****






        share|improve this answer













        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****







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 26 at 23:25









        DimChtzDimChtz

        2,1951 gold badge8 silver badges26 bronze badges




        2,1951 gold badge8 silver badges26 bronze badges





















            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.



















            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해