Reputation: 2587
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?
Upvotes: 20
Views: 43528
Reputation: 1
externalConsole: true, allows me to type input in the external console but the redirection method "args":["<","in.txt"], does NOT
Using: VSCode 1.80.2 with C/C++ Extension from Microsoft v1.16.3 and Clang Apple clang version 14.0.3 (clang-1403.0.22.14.1) Target: x86_64-apple-darwin22.6.0 Thread model: posix
macOS: Ventura 13.5 (22G74)
Upvotes: 0
Reputation: 1
If the code you are debugging requires user input, set external Console to true. after entering input, avoid clicking "x" to close the external Console. Instead, click "-" to minimise the window. Then keep hitting f10 or f11 to continue debugging.
Upvotes: 0
Reputation: 435
Install extension CodeLLDB
Add new configuration CodeLLDB: Launch
Set program
property as "program": "${workspaceFolder}/${fileBasenameNoExtension}"
(optional) Rebuild code
Chose created Launch
config in VS Debug tab. And start it!
Profit!
Video manual
Upvotes: 8
Reputation: 973
simply:-
step1. click on small gear-icon of debugger window.
step2. make "true" to this ["externalConsole": false,] in launch.json file.
step3. and just restart your debugger.
Upvotes: 1
Reputation: 8150
In my case this was a two-step process.
Upvotes: 0
Reputation: 370
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
Upvotes: 10
Reputation: 21
I hope this helps anyone who comes around here:
by (1) setting "externalConsole" to true
and (2) checking (enabling) "Run In Terminal" in Code-Runner Extension configuration, you can plug-in your input to your code by typing the input on the external console, that would pop up when you run your code.
Upvotes: 2
Reputation: 1137
I also encountered the same problem, my solution was to replace cygwin's gdb and g ++ with mingw64's.
Upvotes: 0