【发布时间】:2015-11-18 22:59:04
【问题描述】:
是否可以编写代码使我输出的文本格式化到屏幕中间?我已经尝试了很多,但没有任何效果。这是我目前想到的。
cout.setf (ios::middle);
这是一个错误。 我也试过了
setw(10);//etc.
但是我对使用 setw 命令有点陌生,所以我不知道如何正确使用它。
更新:
//The Game of 4 Seasons
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
cout << "Welcome to this game\n\n\n";
system ("pause");
system ("cls");
cout << "CAUTION!\n\n";
cout << "The adventure you are about to embark contains high levels of:\n";
cout << "Fun\n";
cout << "Adventure\n";
cout << "Excitement\n\n\n";
cout << "If you have a record of buzz killing or anything similar, \nthen this game is NOT for you.\n\n\n\n";
system ("pause");
return 0;
}
【问题讨论】:
-
您不能以独立于平台的方式执行此操作,因为没有标准方法可以知道您的控制台的宽度。
-
我猜你要问的是操作系统,或者你需要某种特殊的库来解决这个问题,比如 ncurses。
-
值得一提的是,
cout转到主要输出,有时是屏幕,有时是文件。写入“文件中一行的中间”并没有多大意义,除非您对每行的长度做出假设。你可以做出这样的假设吗? -
@Ben 好的,我将对问题进行更新,以显示我的具体要求。
-
C++ 对您的“屏幕”一无所知。事实上,我们也没有!