2014-01-14 38 views
1

我目前正在关注由youbube上的newboston创建的教程。我并不是一字不差,但足够接近。system()函数不工作C++

我简单的程序:

#include <iostream> 
#include <string.h> /* memset */ 
#include <unistd.h> /* close */ 
#include <stdio.h> 
#include <stdlib.h> 
#include <cstdlib> 

int main(){ 
    using namespace std; 
    cout << "Those who wander too far off the path of reality. Will be plunged into total Madness." << endl; 
    cout << "                       - BinKill-Ethical" << endl; 
    system("cls"); 
    return 0; 

} 

这是我在C首先++编程,我已经创建。我几乎不知道,但我无法让system()函数工作。

输出:enter image description here

所有除#include <iostream>来自其他计算器职位的建议,试图得到这个工作。没有工作。如果有问题,我正在使用G ++进行编译。

+2

就C++而言,“系统”不保证任何结果。你没有'cls'命令(Windows默认是这样的,这就是本教程的用途)。 – chris

+3

使用'system(“cmd/c cls”);' –

+2

@CaptainObvlious,不是Windows,通过''判断。 – chris

回答

2

system函数用于启动存在于目标平台上的可执行文件。在Windows平台上,cls命令内置于外壳中,并不作为独立可执行文件存在。这使得仅使用system("cls")就无法清除屏幕,因为名为“cls”的可执行文件不是Windows的标准部分。您仍然可以在默认安装的Windows上清除屏幕,但必须通过启动命令shell来完成。

system("cmd /c cls"); 

/c选项指示壳(cmd)来执行命令cls,然后退出。

如果你正在为Windows编写你的程序,我建议看看console API。如果你正在编写你的应用程序的多个平台,我建议看看ncurses。两者都将允许您以更加程序化的方式清除屏幕,而不是仅使用system