clearing the screen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
    int max, death;
    string black = "/roll";
    {
    cout<< " Welcome to the dice rolling game\n";
    cout<< "\n";
    cout<< "To roll the dice simply type /roll\n";
    cout<< "\n";
    cout<< "\n";
    cin>> black;
    cout<< "\n";
    cout<< "\n";
    
    cin.ignore ();
    
    if (black == "/roll")
    {
                 srand (time (0));
                 death = (rand () % 20 );
                 cout<< "\n";
                 cout<< "\n";
                 cout<< "You rolled a .... " << death << "\n";
                 cout<< "\n";
                 }
                 cin.ignore ();
                 cin.get ();
                 
                 }
                 }


Its a simply program, I' am jus wondering how I would clear the screen back to the beginning of the program and not just clearing everything to a blank screen.
You don't. At least not with standard C++.

If you however have a function available that clears the screen, just do that and print the welcome message again.
How would you do that, and can you constantly repeat it back to the welcome message?
closed account (zb0S216C)
As Hanst said, C++ doesn't provide console functionality, it's the OS that provides the functionality. If you don't care about cross-platform development (for consoles), go ahead an use system( "CLS" ). However! a call to system( ) is heavy, ugly and is a security risk; not to mention that it relies on the executables within the System32 folder. A removal of the executable that your program uses, and your program will not run.

Wazzak
@Framework: Why do you say that system() has a security risk? And what do you literally mean by a security risk?
It calls whatever program you tell it to, as Framework said. Someone could easily replace that program with a malicious one, or even simply place it higher on the default paths the OS looks on.
closed account (10oTURfi)
Dont reccomend him system("cls"). It slowes down program alot.

I think Duoas made a nice alternative for that, which is MUCH faster than system("cls") Gonna post it when I find it.
closed account (10oTURfi)
Awesome stuff guys.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
All credits for this goes to Duoas @http://www.cplusplus.com
*/
#include <windows.h>

void ClearScreen()
  {
  HANDLE                     hStdOut;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD                      count;
  DWORD                      cellCount;
  COORD                      homeCoords = { 0, 0 };

  hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  if (hStdOut == INVALID_HANDLE_VALUE) return;

  if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
  cellCount = csbi.dwSize.X *csbi.dwSize.Y;

  if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ',
    cellCount,
    homeCoords,
    &count
    )) return;

  if (!FillConsoleOutputAttribute(
    hStdOut,
    csbi.wAttributes,
    cellCount,
    homeCoords,
    &count
    )) return;

  SetConsoleCursorPosition( hStdOut, homeCoords );
  }
Topic archived. No new replies allowed.