Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;表对齐-cout和iomanip_C++_Alignment_Cout - Fatal编程技术网

C++ C++;表对齐-cout和iomanip

C++ C++;表对齐-cout和iomanip,c++,alignment,cout,C++,Alignment,Cout,我的程序中有一个小的对齐问题 #include <iostream> #include <iomanip> using namespace std; int main() { cout << setw(5) << "Sl. No:" << setw(15) << "Month" << setw(15) << "Name" << endl << endl; co

我的程序中有一个小的对齐问题

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    cout << setw(5) << "Sl. No:" << setw(15) << "Month" << setw(15) << "Name" << endl << endl;
    cout << setw(5) << 1 << setw(15) << "January" << setw(15) << "Abhilash" << endl;
    cout << setw(5) << 2 << setw(15) << "Februaury" << setw(15) << "Anandan" << endl;
    cout << setw(5) << 3 << setw(15) << "March" << setw(15) << "Abhilash" << endl;
    cout << setw(5) << 4 << setw(15) << "April" << setw(15) << "Anandan" << endl;

    return 0;
}

有什么问题吗?

字符串
Sl。否:
是7宽的,您正在尝试将其放入5宽的列中。这会将第一行上推两列。尝试将第一列的宽度设置为7而不是5:

cout << setw(7) << "Sl. No:" << setw(15) << "Month" << setw(15) << "Name"
     << endl << endl;
cout << setw(7) << 1 << setw(15) << "January" << setw(15) << "Abhilash"
     << endl;
//...

cout字符串
Sl.No:
为7宽,您正试图将其放入一个5宽的列中。这会将第一行上推两列。尝试将第一列的宽度设置为7而不是5:

cout << setw(7) << "Sl. No:" << setw(15) << "Month" << setw(15) << "Name"
     << endl << endl;
cout << setw(7) << 1 << setw(15) << "January" << setw(15) << "Abhilash"
     << endl;
//...

cout当您想要使用setw时,您必须从输出字符串的末尾开始计数,int等等

 cout << setw(15) << "January";

cout当您想要使用setw时,您必须从输出字符串的末尾开始计数,int等等

 cout << setw(15) << "January";

cout哦,你需要在“月”前面使用空格

不是

结果如下所示

     Month
    January

您可能需要调整您使用的间距。

哦,您需要在“月”前面使用间距

不是

结果如下所示

     Month
    January

您可能需要调整使用的间距。

非常感谢。感谢您提醒我们初学者可能犯的愚蠢错误。问题解决了,非常感谢。感谢您提醒我们初学者可能犯的愚蠢错误。问题解决了。这个技巧是从左边对齐,如果我们想从右边对齐呢?这个技巧是从左边对齐,如果我们想从右边对齐呢
     Month
    January