How to Align text to the right using cout?

ⅰ亾dé卋堺 提交于 2019-12-11 01:52:51

问题


Suppose I have a string s which is given below:

string s="i am\ngoing\nto\ncuet";

I want to align the string to the right during display in console. So I want to show output like this:

EDIT: rightmost characters should be aligned.

                                      i am
                                     going
                                        to
                                      cuet

I tried this code to show the output:

cout.width(75);
cout<<s;

But it only right align the first line like this:

                                                         i am
going
to
cuet

Then I tried this code to get the output:

for(int i=0 ; i<s.size(); i++)
    {
        cout.width(75);
        cout<<s[i];
    }

But I get peculiar output using this code:

                                                                  i

                                                        a
                                                   m

                                                                  g
                                                             o
                                                        i
                                                   n
                                              g

                                                                  t
                                                             o

                                                                  c
                                                             u
                                                        e
                                                   t

How can I get the desired output?


回答1:


You need to read s line by line, then output each line right aligned.

#include <iostream>
#include <iomanip>
#include <sstream>

void printRightAlignedLines(const std::string& s, int width)
{
    std::istringstream iss(s); //Create an input string stream from s
    for (std::string line; std::getline(iss, line); ) //then use it like cin
        std::cout << std::setw(width) << line << '\n';
}

int main()
{
    std::string s = "i am\ngoing\nto\ncuet";
    printRightAlignedLines(s, 75);
}



回答2:


#include<bits/stdc++.h>
using namespace std;

int main(){

    string s="i am\ngoing\nto\ncuet";
    string p = "";

    for(int i=0;s[i]!='\0';i++){

        p = p+s[i];

        if(s[i]=='\n'){
            cout.width(75);
            cout<<p;
            p = "";
            continue;
        }

        if(s[i+1]=='\0'){
            p = p+'\n';
            cout.width(75);
            cout<<p;
            p = "";
        }


    }


}



回答3:


#include <iostream>
#include <string>
int main (){
    std::string s="i am\ngoing\nto\ncuet";

    std::cout.width(75);
    for(int i=0 ; s[i]!='\0'; i++)
    {
            if(s[i]=='\n'){
                   std::cout<<s[i];
                   std::cout.width(75);
            }else{
                   std::cout<<s[i];
            }
    }
    std::cout <<std::endl;;
}

Here this should work. :D



来源:https://stackoverflow.com/questions/43033802/how-to-align-text-to-the-right-using-cout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!