Duong Nguyen Quy
Duong Nguyen Quy

Reputation: 71

How to pause a console c++ application when it finished in Visual studio code

I want to pause the console c++ program after it finished to watch its result. I can't find any attribute launch configuration to pause my program. Here is my code in launch.json.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(Windows) build and launch",
            "type": "cppvsdbg",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true
        }
    ]
}

Upvotes: 3

Views: 3206

Answers (3)

rustyx
rustyx

Reputation: 85531

Put a breakpoint at the end of main():

Then run under debugger (F5). It'll stop just before program exit.

Upvotes: 1

Duong Nguyen Quy
Duong Nguyen Quy

Reputation: 71

thanks for your help. here's my solution: i run a cmd within the command to run my program and pause when its done

    {
        "name": "(Window) build and run",
        "type": "cppvsdbg",
        "request": "launch",
        "preLaunchTask": "build",
        "program": "cmd",
        "args": [
            "/k", 
            "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "&",
            "pause",
            "&",
            "exit"
        ],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true
    }

Upvotes: 4

WOLFdesigne
WOLFdesigne

Reputation: 1

system("pause"); //this is what I use

cin.get(); //you can use this, but when you press ENTER, the console closes, if you enter a variable it is not a good solution

Upvotes: 0

Related Questions