即使正确的标头,System(“ pause”)也不起作用

时间:2018-10-03 14:57:42

标签: c++

我在需要在Visual Studio中多次使用system(“ pause”)的情况下制作此程序,但是无论何时运行代码,它都不会暂停。我有<cstdlib>标头以及其中的所有内容。还有其他原因为什么这行不通?谢谢!

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <stdlib.h>


using namespace std;

int main() {
    int range = (rand() % 5) + 1;
    double start = 0;
    double end = 0;

    cout << "Try to hit the same key within " << range << "seconds.";

    system("pause");
    start = clock();

    system("pause");
    end = clock();

    system("pause");
    return 0;
}

2 个答案:

答案 0 :(得分:0)

这实际上对我来说很好(在Windows上),但是如果您只想读键盘输入,然后尝试一下(应该在所有地方都可以使用):

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

int main() {
    int range = (rand() % 5) + 1;
    double start = 0;
    double end = 0;

    cout << "Try to hit the same key within " << range << "seconds.";

    int c = getc (stdin);
    start = clock();

    c = getc (stdin);
    end = clock();

    c = getc (stdin);
    return 0;
}

Live demo

答案 1 :(得分:0)

“暂停”不是您可以单独运行的程序,它是命令提示符下的功能。如果必须使用系统调用来执行此操作,请运行:

cmd.exe /c pause