Search code examples
c++ioc++17clion

Clion `cout` and `cin` combination causes console to not work properly


I've recently started trying out CLion for C++ programming. I wanted to test a sample application (below):

#include <iostream>

int main()
{
    std::cout << "Please enter a number: ";
    int x;
    std::cin >> x;

    std::cout << "Your number was " << x << "!\n";
    return 0;
}

This is what I was expecting (the number is user input):

Please enter a number: 10
Your number was 10!

And this is exactly what happens when I compile and run manually (g++ main.cpp -o main && ./main) However, this is what happens when I run with CLion:

input and output not working properly

Does anyone know why this is happening, and how I can fix this?

Note: I am using CLion with the g++ compiler (version 9.3.0) on WSL2


Solution

  • After some more searching, I came across this StackOverflow post, which led me to this issue (upvote it!) which finally led me to do what was told in the comments:

    Two workarounds are available:

    • Turn off PTY: by disabling run.processes.with.pty option in the Registry (Help -> Find Action -> Registry...)
    • Use Cygwin64 instead

    I did the first option and CLion works fine now:

    CLion working

    It looks like this is an issue with MinGW and WSL.