How do I use system("CLS") without using system("CLS")?

Beneath is my (almost) completed first C++ project (self-made). As you see in my main_page function, I try to have a sort of main screen introducing the game, then counting down from 5 to 0, then leading onto the game itself.

I want to be able to have it go "It will begin...5" with the "5" changing to 4, 3, 2, 1, then going onto the main function. But with system("CLS") it clears the whole main_page function and counts down 4, 3, 2, 1 all by itself with no other text.. how can I change this? Anyone know the code for me to be able to keep the first text while it counts down from 5 to 0?

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;

void main_page()
{
    int x = 5;

    cout << "-----------------------------------DICE GAME-----------------------------------\n"
    << "This is my first C++ Program\n" << "It will begin in...";

    Sleep(3000);

    for (int x = 5; x >= 0; x--)
    {
        Sleep(1000);
        cout << x << endl;
        system("CLS");
        cout << x << endl;
    }
        system("CLS");
}

int main()
{
    main_page();
    string roll_or_quit;
    int one = 0, two = 0, three = 0, four = 0, five = 0, six = 0;
    int want_to_roll;
    int die_roll;

    cout << "Type 'roll' to roll, or 'quit' to quit\n";
    cin >> roll_or_quit;
    cin.ignore();

    if (roll_or_quit != "roll")
    {
        cout << "PROGRAM TERMINATED";
    }

    while (roll_or_quit == "roll" && want_to_roll != -1)
        {

        while (want_to_roll != -1)
            {
            cout << "How many times do you want to roll?\n(cant be more than 6) or type -1 to finish: ";
            cin >> want_to_roll;
            cin.ignore();

            srand(time(0));

            for (int x = 0; x < want_to_roll; x++)
            {
                die_roll = 1 + (rand()%6);
                cout << die_roll << endl;

                switch (die_roll)
                {
                    case 1:
                        one++;
                        break;
                    case 2:
                        two++;
                        break;
                    case 3:
                        three++;
                        break;
                    case 4:
                        four++;
                        break;
                    case 5:
                        five++;
                        break;
                    case 6:
                        six++;
                        break;
                    default:
                        cout << "The program had an error!";
                }
            }
        }
        cout << "The total times the number one came up: " << one << endl;
        cout << "The total times the number two came up: " << two << endl;
        cout << "The total times the number three came up: " << three << endl;
        cout << "The total times the number four came up: " << four << endl;
        cout << "The total times the number five came up: " << five << endl;
        cout << "The total times the number six came up: " << six << endl;
    }
    return 0;
}
Last edited on
@Lansana

You could go this way..
1
2
3
4
5
6
7
8
for (int x = 5; x >= 0; x--)
    {
        cout << x;
         Sleep(1000);
        cout <<"\b \b"; // The "\b" is the escape code to back up one space, then the space after the \b will delete
// the number on screen. Then there is the second "\b" to put the cursor where the number is to be printed.
// It'll do it the 5 times ( actually six because of the zero) and you have a count down 
    }
closed account (Dy7SLyTq)
since you use cls im going to assume that your on windows
*taken from Duoas' infamous article on 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
30
31
32
33
34
35
36
37
38
#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;

  /* Get the number of cells in the current buffer */
  if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
  cellCount = csbi.dwSize.X *csbi.dwSize.Y;

  /* Fill the entire buffer with spaces */
  if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ',
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Fill the entire buffer with the current colors and attributes */
  if (!FillConsoleOutputAttribute(
    hStdOut,
    csbi.wAttributes,
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Move the cursor home */
  SetConsoleCursorPosition( hStdOut, homeCoords );
  }
@DTS: That shows him how to clear the screen, but not how to keep his cout<<"--DICE GAME---";

Whitenite's way works well, another way is:
1
2
3
4
5
6
7
8
for (int x = 5; x >= 0; x--)
    {
        Sleep(1000);
system("CLS");//I highly recommend you use DTSCode's method of Clearscreen(); instead, infinitely faster (and safer)
        cout<<"----Dice game----";
        cout<<"The game will start in... ";//essentially re-output your top bit each time after it gets erased.
        cout << x << endl;
    }
Topic archived. No new replies allowed.