如何将输出设置为中间

How to set output to middle?

本文关键字:中间 设置 输出      更新时间:2023-10-16

是否可以编写代码将输出的文本格式化为屏幕中间?我试了很多,但都没用。以下是我到目前为止的想法。

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 gamennn";
system ("pause");
system ("cls");
cout << "CAUTION!nn";
cout << "The adventure you are about to embark contains high levels of:n";
cout << "Funn";
cout << "Adventuren";
cout << "Excitementnnn";
cout << "If you have a record of buzz killing or anything similar, nthen this game is NOT for you.nnnn";
system ("pause");
return 0;
}

Q:是否可以编写代码将输出的文本格式化为屏幕中间?

A: 是的。而不是直接用"cout"。但当然有类似ncurses:的东西

  • http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/

  • http://invisible-island.net/ncurses/ncurses-intro.html

就我自己而言,我更喜欢诅咒。

但根据您的需求有多复杂,您可能会考虑ansi终端模拟。。。大多数系统都有它们。(在Ubuntu上,它被称为gnome终端")

然后您可以使用ansi终端控制进行输出。例如,

void gotoxy(int col, int row)

可以输出esc字符,后跟"["和行(即"12"),后跟";"和列号("40")后面跟着"H"。

用户输入将是std::cin。

这不是一个很好的解决方案,但有一些功能。