Not use system()... but how?

Pages: 12
I know that system() is evil and stuff (and why), but how do you not use it?
I am starting to create a game but need to know stuff like how to pause and clear the screen by NOT using system(). It would as well be wondrous to inform me on other stuff like changig text colour, changing title, and much, much more.
Thanks if you may leave a reply leting me know about how to do a bunch of stuff without using system().

-legit
how to pause and clear the screen by NOT using system().


C++ knows nothing about screens, colours, and all that sort of thing. All that is done by your operating system. You do it by asking the operating system to please do it.

You can do that directly by learning your operating system's function calls (the API), or you can use a "widget toolkit" which gives you a simpler set of functions to call, and it calls the operating system functions for you.
Thanks bro. But that doesn't completely help...
I use a for loop that outputs a series of newline characters in place of clear and cin or getchar() for pause. Changing the text color is a bigger issue (search for it on this site; there's lots of info). You can use SetConsoleTitle to change the title. Is this more what you are looking for?
Sorta yeah. The only problem is I'm only 10 yrs old, a beginner, and would rather see this in actual code. Thanks anyways though :)
use cin.get(); to pause it does the exact same thing. as for clearing the screen im not too sure, i alwaysed used system("cls"); for that but idk everyone has their own tricks.

as for dawtsf1187 method, it looks like this:

1
2
3
4
for(int i = 0; i < 25; i++) //this will output 25 newline characters
{
    cout << "\n";
}
Last edited on
It depends on the operating system, and whether you want portable cross-platform code, or just something which works for you.

Windows compilers may offer the <conio.h> library, which, if available may do what you want. Otherwise you may have to do slightly more complex Windows API calls.

Another, completely different option is the ncurses library (Google it).
You aren't legit until you start to learn how to research stuff for yourself and by not complaining that good answers aren't helpful.

Despite all this, I will give you some very, very good advice, split into two different options:

1) Forget the console. It is more grief than it is worth. Look into SDL or SFML.

A good way to get familiar with SDL is by playing with PyGame.
http://www.pygame.org

2) Use Curses.

NCurses (for POSIX) http://www.gnu.org/software/ncurses/
PDCurses (for Windows) http://pdcurses.sourceforge.net/

Learn how to use Curses
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/

There are no shortcuts here, alas.
Good luck!
You could make a function that clears the screen. I have created one if you would like.
its a c++ function all you do is copy and past the function into your code and every time you want to clear the screen you simply call the function clr_src();
and boom done.
Last edited on
@Westonrwright i would like to see the code please.
if he doesnt give it to u search ncurses. but now i see everyone else suggested that. system is evil because it has a huge security hole, which i dont see how you could get around in any api, and it is a huge memory waste
I wasn't clear enough in my answer. I'll repeat it, but this timeI'll make it easier by putting the keywords that should be googled in bold text.

C++ knows nothing about screens, colours, and all that sort of thing. All that is done by your operating system. You do it by asking the operating system to please do it.

You can do that directly by learning your operating system's function calls (the API), or you can use a "widget toolkit" which gives you a simpler set of functions to call, and it calls the operating system functions for you.


Someone familiar with google will be able to use the bold text to find, for example, the wiki list of widget toolkits.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<string.h>
#include<conio.h>
#include<windows.h>

void clr_scr()
{
	HANDLE hndl=GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	GetConsoleScreenBufferInfo(hndl, &csbi);
	DWORD written;
	DWORD N= csbi.dwSize.X*csbi.dwCursorPosition.Y+csbi.dwCursorPosition.X+1;
	COORD curhome={0,0};

	FillConsoleOutputCharacter(hndl, '  ', N, curhome, &written);
	csbi.srWindow.Bottom-=csbi.srWindow.Top;
	csbi.srWindow.Top=0;
	SetConsoleWindowInfo(hndl, TRUE, &csbi.srWindow);
	SetConsoleCursorPosition(hndl,curhome);
}

Ill be honest i dont understand 100% of this. This is what i use on Microsoft Visual Studio. I dont know anything about Windows Console.
Last edited on
I feel like my solution has less if's and is a little more simple
Feel what you want. Your solution is not correct.
That's a useless comment.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//diff
a> cellCount = csbi.dwSize.X *csbi.dwSize.Y; //all screen
b> N = csbi.dwSize.X*csbi.dwCursorPosition.Y+csbi.dwCursorPosition.X+1; //upwards screen

a>  /* Fill the entire buffer with spaces */
  if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ', //casting
    cellCount,
    homeCoords,
    &count
    )) return;
b> FillConsoleOutputCharacter(hndl, '  ' /*2 spaces*/, N, curhome, &written);

a> /* Fill the entire buffer with the current colors and attributes */
b> /* Nothing */

a> /* Nothing */
b>	csbi.srWindow.Bottom-=csbi.srWindow.Top;
	csbi.srWindow.Top=0;
	SetConsoleWindowInfo(hndl, TRUE, &csbi.srWindow);
And error handling (important).

¿What's the purpose of the last snip?
This is something I pulled from Duoas (who I would like to personally thank for saving me multiple times on here!):
1
2
3
4
5
6
7
8
9
#include <stdio.h>

void PressEnterToContinue()
{
  int c;
  printf( "Press ENTER to continue... " );
  fflush( stdout );
  do c = getchar(); while ((c != '\n') && (c != EOF));
} //end PressEnterToContinue 

This will take care of ANY TIME THE PROGRAM IS ABOUT TO CLOSE. The problem, however, still remains with how to nicely (and safely!) clear the screen. I would say go with the above advice on that one.
Im sorry my solution is wrong? how is it wrong since it uh.. well works perfectly and clears the screen? This was given to my by my computer science professor and ive used it in multiply programs. Its not "not correct".
Last edited on
How about just do this:

1
2
3
4
5
6
7
8
9
#include <iostream>

using namespace std;

int main()
{
     cout << "/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/";
     cout << "/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/";
}


It's pathetic but, hey. It works.
Pages: 12