sclchina.com

How to Clear Screen Before a New Loop in a CPP

1
    • 1). Open the CPP program in a text editor. At the very top of the program, where program library includes are declared (right before main()), declare that you desire the CPP compiler's pre-processor to also include the C standard library, labeled "cstdlib." To do so, enter the following line on a blank line by itself:

      #include <cstdlib>

    • 2). Locate the end of the loop where the command to clear the screen is desired. The best place to put the clear screen command is immediately following an instruction that is the last thing the user does when interacting with the CPP program. For example, if the loop ends with a code block asking the user to type "Y" to start the program again: if (startOver == "Y") ,then place the system clear command in that program restart block. Such a block will typically call the first function of the program. If so, place the screen clear command immediately before the call to the top level function.

    • 3). Determine the operating system the program will be desired to be compiled and run on. The Linux and Apple operating systems both have underlying UNIX infrastructure within them. For these two operating systems, "clear" is typed at the command prompt to clear the console screen. For Microsoft operating systems, "CLS" is entered at the command prompt to clear the console screen.

    • 4). Enter the correct clear screen command at your desired location and on a separate line. If the program will be compiled to run on Linux or Apple, enter the following:

      system("clear");

      Do not forget the semicolon at the end of the line.

      If the program will be compiled to run on a Microsoft operating system console, enter the following: system("CLS");

      Again, do not forget the semicolon at the end of the line.

    • 5). Save the CPP program and then compile the program. Run the program and test to make sure it operates the way you expected with the clear screen command placed where you have put it. Edit the program code and move the command as needed until your program operates as desired, if it does not behave as planned.

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.