"srand", "rand()%10" and clean screen

How I should use them?

[edit] Found on the site the srand an rand functions.

And how I can clear the screen? [edit] (I need of this to make my text based game...)

Last edited on
Call srand() once at the beginning of your game:
1
2
3
4
5
6
7
#include <cstdlib>
#include <ctime>

int main()
  {
  srand( time( NULL ) );
  ...

Then use rand() to get a pseudo-random number whenever you need it. Using % 10 means to clamp the returned random number to the range 0..9.

If you are doing a full-blown console game, look into the Curses library
NCurses
platform: POSIX
where: http://www.gnu.org/software/ncurses/
If you are on Unix, you've probably already got it installed.
If you are on Linux, use sudo apt-get install ncurses-dev or somesuch.
Link with -lcurses (on Solaris link with -lncurses).

PDCurses
platform: DOS, Win32, OS/2, Plan 9, SNES, etc.
where: http://pdcurses.sourceforge.net/
Works nicely with MinGW.
Link with -lpdcurses.

An excellent getting-started guide: http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/

Hope this helps.
Wow, thanks! But... How I can clean the screen?
You can do system("CLS"); but professionals on this forum don't like it because it is hated by the Antivirus and is slow and leaves a BIG, BAD, GAPING security hole.
Other people suggest you can print out 100 lines so instead of clearing the screen it just pushes the text all the way up to the top (I forgot how to do that :P)
cout << string (100, '/n'), correct?
Last edited on
Topic archived. No new replies allowed.