简体   繁体   中英

How to set output to middle?

Is it possible to write code to make my outputted text formatted to the middle of the screen? I have tried a lot, but nothing has worked. Here is what I have thought of so far.

cout.setf (ios::middle);

That was an error. Also I tried

setw(10);//etc.

But I'm kind of new to using the setw command so I don't know how to use it properly.

UPDATE:

//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;
}

Q: Is it possible to write code to make my outputted text formatted to the middle of the screen?

A: Yes. Not with "cout" directly. But certainly with something like ncurses:

For myself, I prefer curses.

But depending on how complex your needs are, you might consider ansi terminal emulation ... most systems have them. (On Ubuntu, it is called gnome-terminal")

Then you could use ansi terminal control for output. For example,

void gotoxy(int col, int row)

could output an esc char, followed by "[" and the row (ie "12"), followed by ";" and the col number ("40) followed by "H".

User input would be std::cin.

Not a wonderful solution, but with some functionality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM