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().
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.
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?
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";
}
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 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.
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.
#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.
//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);
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".