John Smith
John Smith

Reputation: 19

System("pause") isn't working even with correct header

I'm making this program where I have to use system("pause") a few times in Visual Studio, but whenever I run the code, it doesn't pause at all. I have the <cstdlib> header and everything in there. Is there another reason why this wouldn't work? Thanks!

#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;
}

Upvotes: 1

Views: 2839

Answers (2)

HerrJoebob
HerrJoebob

Reputation: 2313

"pause" isn't a program you can run by itself, it's a function of the command prompt. If you must do this using a system call, instead run:

cmd.exe /c pause

Upvotes: 1

catnip
catnip

Reputation: 25388

That actually works fine for me (on Windows), but if all you want to do is read a keystroke then try this (should work everywhere):

#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

Upvotes: 0

Related Questions