C ++ debug mode in eclipse makes the program not wait for cin
The following code works fine when it runs, but there is a problem when it runs in debug mode with Eclipse, it doesn't wait for input and instead just keeps moving and some seemingly random value is printed to the console. It also won't stop at a breakpoint.
int main() {
int N, Q, maxSize;
cout <<"Enter a number"<<endl;
int test;
cin >> test;
cout << test <<endl;
}
source to share
Update
As of CDT 9.4 (Eclipse Oxygen.2) there is now a checkbox in the launch config to do this with one click. See https://wiki.eclipse.org/CDT/User/NewIn94#Debug
Original Answer
The problem here is that there are two readers on the same stdin pipe. When you do cin
Eclipse CDT also tries to read stdin for GDB-MI communication.
Fortunately, there is a workaround, you can create a separate console for the program to run in GDB. This means no pen sharing.
To do this, create a file .gdbinit
in the root of the project with this content:
set new-console on
and debug your console app in Eclipse down to your heart content:
Additional Information
You can install the gdbinit file for the Debug Configuration in the Debugger tab. Set the GDB command file to the name of the file you created.
You can set the default GDB command file for newly generated Debug configurations by editing the preference in the C / C ++ -> Debug -> GDB page:
Eclipse CDT does not use .gdbinit in your home directory. This is on purpose because .gdbinit, which is generally configured for CLI debugging and can easily interfere with the MI intuition Eclipse needs to communicate with GDB correctly.
source to share