What is the C++ code for system("cls")

What is the C++ code for system("cls")
I hear people saying that you should never use system in a C++ program
There isn't AFAIK a standard equivalen because the console isn't supposed to be cleared.

If you must, though, Duoas wrote an article about the topic which you can find here:
http://www.cplusplus.com/forum/articles/10515/

Happy programming. :)

-Albatross
Saying that you should "Never use system" is wrong, there is no "other way to look at it" it is an incorrect statement. The "System()" function works perfectly for what it does and that is pass commands to the shell, in this case it passes "cls" which to 'cmd.exe' means clear the screen. It's true that it's not the best tool for the job but you should always be aware that it exists.

Like Albatross said it is also true that the console, in most cases, should not be cleared so that's something to consider as well.
Last edited on
system() is a C leftover. But it doesn't just come from a different place, it comes from a different time. As it's already been said, you shouldn't (need to) use it.
@Catfish, well in my case if i dont use it, the screen will get real ulgy real fast
The Windows console supports redirection.
So instead of letting your program vomit its output on the console, you could start it like:
prog.exe > output.txt

and that'd save its output to a text file in the same directory as the executable.
look heres my code, i dont need output.txt because i allready hid the output

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
#include "iostream" 
#include "conio.h"
#include "string"

using namespace std;

int Addition(int x, int y);
int Subtraction(int x, int y);
int Division(int x, int y);
int Multiply2(int x, int y);

int main(){
	int num1;
	int num2;
	string mathType;
do{
	cout << "Would you like to add, subtract, divide, or multiply : " << endl;
	cin >> mathType;
	cout << "Enter the first number you wish to " << mathType << ":" << endl;
	cin >> num1;
	cout << "Enter the second number you wish to " << mathType << ":" << endl;
	cin >> num2;
	system ("cls");

	if (mathType =="add"){
		cout << num1 << " + " << num2 << " = " << Addition(num1, num2) << endl;
	}
	if (mathType == "subtract"){
		cout << num1 << " + " << num2 << " = " << Subtraction(num1, num2) << endl;
	}
	if(mathType == "divide"){
		cout << num1 << " - " << num2 << " = " << Division(num1, num2) << endl;
	}
	if(mathType == "multiply"){
		cout << num1 << " * " << num2 << " = " << Multiply2(num1, num2) << endl;
	}
	_getch();
	system("cls");
}while(1);
}

int Addition(int x, int y){
	int numAnswer;
	numAnswer = x + y;
	return numAnswer;
}

int Subtraction(int x, int y){
	int numAnswer;
	numAnswer = x - y;
	return numAnswer;
}

int Division(int x, int y){
	int numAnswer;
	numAnswer = x / y;
	return numAnswer;
}

int Multiply2(int x, int y){
	int numAnswer;
	numAnswer = x * y;
	return numAnswer;
}



I can see a little #include "conio.h" up there.

You might as well use clrscr() instead of system("cls") if you included that abomination of a header.
Last edited on
just suggest a mod. need to check for zero. and it can be shortened even more then that by moving the math right into the cout function. but im assuming your practicing functions so..

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
int Addition(int x, int y)
{
	return x + y;
}

int Subtraction(int x, int y)
{
	return x - y;
}

int Division(int x, int y) 
{     // dont divide by zero
       if (int x = 0 || y == 0)
           return 0;

       return x / y;
}

int Multiply2(int x, int y)
{     // anything times zero is zero
	if ( x == 0 || y == 0)
         return 0;

         return x * y;
}

@catfish, ok thats what i was asking in the first place, clrscr() does the same thing as system("cls")

@acron, no i will put in my own error handing later.
Last edited on
Topic archived. No new replies allowed.