system()函数不能正常工作C ++

时间:2014-01-14 02:42:56

标签: c++ system

我目前正在关注youbube上由thenewboston创建的教程。我没有逐字逐句地跟踪,但足够接近。

我的简单程序:

#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>之外的所有内容都是来自其他stackoverflow帖子的建议,以尝试使其正常工作。没有人工作过。如果重要的话,我正在使用G ++进行编译。

2 个答案:

答案 0 :(得分:2)

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

system("cmd /c cls");

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

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

答案 1 :(得分:0)

如果您使用的是Linux,则可以改用以下内容:

system("clear");

并且假设您想在打印之前清除屏幕,您的代码将变为:

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

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

}
相关问题