Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read input when debugging in C++ in Visual Studio Code?

I'm using VSCode for debugging my CPP program in MacOSX.

I've 2 programs.

Program1

int main(){

    string a;
    a = "a";
    a += 'b';
    cout<<a<<endl;
    return 0;
}

Program2

int main(){

    string a;
    cin>>a;
    a += 'b'
    cout<<a;
    return 0;
}

In program1 I'm directly assigning the string a and when I debug the program in VSCode by first compiling it in terminal using :

g++ -g filename.cpp

and then selecting the Starting Debugging option in the Debugging menu. I'm able to see the state of the string a variable by moving forward in breakpoints.

The VARIABLES section shows the state of different variables and the CALL STACK show the stack frame.

But, for program2, when I go past the breakpoint of the cin>>a;, the contents of VARIABLES and of CALL STACK get cleared up.

Here are the contents of the launch.json file:

{
    "version": "0.2.0",
    "configurations": [    
        {
            "name": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb"
        }
    ]
}

How can I get user-input and move forward to debug my code?

like image 600
ajaysinghnegi Avatar asked Aug 14 '19 12:08

ajaysinghnegi


2 Answers

As stated in Here

if you enable "externalConsole":true in the launch.json then you will get a pop up console window that you can type in.

like image 96
ajaysinghnegi Avatar answered Oct 13 '22 22:10

ajaysinghnegi


To debug with inputs, you can edit the arg section as shown below:

"program": "${workspaceFolder}/main",
"args": ["<", "input_file.in"]

The example above should be the same as: ./main < input_file.in

like image 27
Joseph Pena Avatar answered Oct 13 '22 22:10

Joseph Pena