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?
-
1Not sure if it is the same for VSCode but in VS when you step over input in the debugger you can switch over to the console window and type in the input, press enter, and then go back to stepping through the code.NathanOliver– NathanOliver2019年08月14日 12:37:38 +00:00Commented Aug 14, 2019 at 12:37
-
@Jos Place a breekpoint to this statement a += 'b'Vlad from Moscow– Vlad from Moscow2019年08月14日 12:37:38 +00:00Commented Aug 14, 2019 at 12:37
-
@VladfromMoscow Yes, I did that and the debugging pauses there. Where can I provide my inputs?asn– asn2019年08月14日 12:40:32 +00:00Commented Aug 14, 2019 at 12:40
-
@Jos Use the console window.Vlad from Moscow– Vlad from Moscow2019年08月14日 12:41:09 +00:00Commented Aug 14, 2019 at 12:41
-
1@rioV8 Unfortunately, that's NOT.asn– asn2019年08月15日 06:13:05 +00:00Commented Aug 15, 2019 at 6:13
9 Answers 9
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.
5 Comments
./program < input.txt. Is there an alternative for that?"console": "integratedTerminal"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
Comments
Install extension CodeLLDB
Add new configuration
CodeLLDB: LaunchSet
programproperty as"program": "${workspaceFolder}/${fileBasenameNoExtension}"(optional) Rebuild code
Chose created
Launchconfig in VS Debug tab. And start it!
Profit! Demo Video manual
1 Comment
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.
Comments
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.
Comments
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.
2 Comments
I also encountered the same problem, my solution was to replace cygwin's gdb and g ++ with mingw64's.
1 Comment
In my case this was a two-step process.
- Enable externalConsole: true, as described in other responses.
- Let VS code control the terminal.
Comments
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-1403022.14.1) Target: x86_64-apple-darwin22.6.0 Thread model: posix
macOS: Ventura 13.5 (22G74)