1

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;
}
Andrew Tew
  • 47
  • 1
  • 8
  • 1
    You can't do that in a platform independent way, as there is no standard way of knowing how wide your console is. – roeland Nov 18 '15 at 23:02
  • My guess is that what you're asking would be OS depended, or you would need some kind of special library for this like ncurses. – 101010 Nov 18 '15 at 23:03
  • It's worth mentioning, `cout` goes to the primary output, which is sometimes the screen, and sometimes a file. Writing to the "middle of a line in a file" doesn't really make much sense, unless you make assumptions about how long each line is. Are you ok with making such assumptions? – Mooing Duck Nov 18 '15 at 23:03
  • @Ben ok, I will make an update to the question showing what I am asking specifically. – Andrew Tew Nov 18 '15 at 23:05
  • C++ doesn't know anything about your "screen". In fact, neither do we! – Lightness Races in Orbit Nov 18 '15 at 23:11
  • @LightnessRacesinOrbit Well by "screen" I mean the C++ output box. The black box from Visual Studio? Yeah, that one. – Andrew Tew Nov 18 '15 at 23:11
  • @roeland I'm assuming that visual studio knows how wide the output box is though. – Andrew Tew Nov 18 '15 at 23:14
  • There is no such thing as a "C++ output box"; your C++ program's output could go to pretty much any terminal, or into a file, or piped into another process, or into `/dev/null`, or printed onto tape .... it's a sequence of bytes. At least now that we know which one you are personally working with, we could suggest non-portable solutions dedicated to working with that particular output .... but none of them have anything to do with C++ the language! And while I recognise that to you this will all appear to be nitpicking, I assure you it's not. :) – Lightness Races in Orbit Nov 18 '15 at 23:19
  • Yeah @LightnessRacesinOrbit I didn't mean C++ as a language. I have never worked with C++ on another program so I just generalized it as a C++ box lol. – Andrew Tew Nov 18 '15 at 23:21
  • If you could now add the details of your terminal into the question that'd make it complete - although you'll find that this has been asked and answered before... – Lightness Races in Orbit Nov 18 '15 at 23:22

2 Answers2

3

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:

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • @Andrew Tew: One additional option is [PDCurses](https://jdonaldmccarthy.wordpress.com/2014/09/05/how-to-set-up-pdcurses-in-visual-studio-2013-c/). Like NCurses, it's also available on all platforms (including Windows). – paulsm4 Nov 18 '15 at 23:18
  • Could you show me the specific page that shows me how to output to the middle in ncurses at least. Or what it is in C++ lol. – Andrew Tew Nov 18 '15 at 23:18
1

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 (i.e. "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.

2785528
  • 5,438
  • 2
  • 18
  • 20