I was trying to run a connect four game in C++, but I got this error while compiling in Cygwin:
c4.cpp: In member function �void connectFour::clearScreen()�:
c4.cpp:162:35: error: �system� was not declared in this scope
system("clear");
^
The coder left this above the part using "system("clear")":
//Set for OSX currently - Change the inner system call for Windows or Linux
How can I solve this problem?
At the top of the program,
#include <stdlib.h>
That will fix the error. Whether it will work I am less certain. The contents of system() are system-dependent. cygwin might have a clear utility or might not. If this was a Windows commandline program rather than cygwin you'd probably do system("CLS");
Adding to Corona's reply...
I am not sure that clear exists in CygWin.
If it does it will be "clear.exe" inside the "/bin" folder(/drawer/directory)...
You could try this as an alternative:-
#include <cstdlib>
int main()
{
system("printf '\033[2J\033[H'"); // This is the important line...
return 0;
}
Compiled inside OSX 10.7.5, default bash terminal...
EDIT:
Forgot to add that compiling this under OSX required:-
#include <cstdlib>
You do not need system() to print escape codes in C:
printf("\x1b[2J\x1b[H"); fflush(stdout);
1 Like
Remember! To use the "printf()" function then......
#include <stdio.h>
OR
#include <cstdio>
......will be needed...
Or else this error will result:-
AMIGA:barrywalker~> g++ clear.cpp
clear.cpp: In function �int main()':
clear.cpp:8: error: �printf' was not declared in this scope
AMIGA:barrywalker~> _
1 Like
When you call system("some_command") -
You are invoking a new process, and the new process may not be running the same shell you connected with. In cygwin that shell is: /bin/sh, which is bash. The other point is it may not be able to find command executables like /bin/clear unless you add the full file name.