程序中的分隔符

时间:2018-01-31 19:02:13

标签: c++ divide

我试图在我的程序中得到一个分隔符,我得到它输出但是我注意到它们是顶部的差距。例如我的代码输出:

*******************************************************************************************

                                                  Broome
                                                  CST 113 Y01
                                                     LAB 
                                                  Stan
******************************************************************************************

我只想让代码输出如下:

  *******************************************************************************************                   
                                                  Broome
                                                  CST 113 Y01
                                                     LAB 
                                                  Stan
******************************************************************************************
没有那个恼人的差距。这是我的输出分隔符:

//Output the divider to the screen
cout << setfill('*') << setw(SCREEN_WIDTH + 1) << " " << setfill(' ') 
     << endl;

// Output the course heading to the screen
cout << setw((SCREEN_WIDTH + 11) / 2) << COLLEGE << endl;
cout << setw((SCREEN_WIDTH + 11) / 2) << COURSE << endl;
cout << setw((SCREEN_WIDTH + 5) / 2) << LAB_NAME << endl;
cout << setw((SCREEN_WIDTH + 15) / 2) << PROGRAMMER_NAME << endl;

//Output the divider to the screen
cout << setfill('*') << setw(SCREEN_WIDTH + 1) << " " << setfill(' ') << endl;

我确信这是一件我很想念的事情

1 个答案:

答案 0 :(得分:0)

我使用MinGW复制您的问题并在80列Windows控制台中运行,并将SCREEN_WIDTH设置为80.如果控制台宽于SCREEN_WIDTH + 1,则不会出现问题。

您的代码将精确打印SCREEN_WIDTH星号,后跟空格。如果您的控制台正好是SCREEN_WIDTH列宽,那么该空间实际上将打印在下一行(尽管您看不到它 - 它是一个空格!)。即使你减去一个,空格仍然会在行的最后一列,导致光标掉线。 endl会再次这样做。

我能够得到与你正在寻找的结果非常接近的结果:

 cout << setfill('*') << setw(SCREEN_WIDTH) << "\n" << setfill(' ');

这是一个完整系列的星号,但这是你能做的最好的。如果您知道将始终使用SCREEN_WIDTH列完全在同一控制台类型上运行,则可以使用空引号而不是"\n"。但这是假设很多控制台。

我使用的结果代码:

#include <iostream>
#include <iomanip>

using namespace std; // BLECH!!

int main()
{
    const auto SCREEN_WIDTH = 80;
    const auto COLLEGE = "Broome";
    const auto COURSE = "CST 113 Y01";
    const auto LAB_NAME = "LAB";
    const auto PROGRAMMER_NAME = "Stan";

    //Output the divider to the screen
    cout << setfill('*') << setw(SCREEN_WIDTH) << "\n" << setfill(' ');

    // Output the course heading to the screen
    cout << setw((SCREEN_WIDTH + 11) / 2) << COLLEGE << endl;
    cout << setw((SCREEN_WIDTH + 11) / 2) << COURSE << endl;
    cout << setw((SCREEN_WIDTH + 5) / 2) << LAB_NAME << endl;
    cout << setw((SCREEN_WIDTH + 15) / 2) << PROGRAMMER_NAME << endl;

    //Output the divider to the screen
    cout << setfill('*') << setw(SCREEN_WIDTH) << "\n" << setfill(' ');
}

输出:

*******************************************************************************
                                       Broome
                                  CST 113 Y01
                                       LAB
                                           Stan
*******************************************************************************