python subprocess, for some reason it doesn't stdout.

problem

For example, suppose you create an exe file with such contents in C ++.

#include <iostream>

using namespace std;


int main(){
    int A,B;
    cout << "Please enter A"
    cin >> A;
    cout << "Please enter B"
    cin >> B;
    
    cout << A+B;
}

It is a very simple program that adds two numbers entered.

When this is executed by python subprocess, it works as follows.

 

??????

Nothing is displayed ... What is this ...

So, nothing is displayed, but try entering 10 and 20. Then,

$ 10
$ 20
$Please enter A Please enter B 30

Wow. If something I wanted to display at the very end is displayed.

solution

This has to be solved in C ++. ** A line break is always required on each line of standard output. Otherwise subprocess will not recognize the output. ** **

So

#include <iostream>

using namespace std;


int main(){
    int A,B;
    cout << "Please enter A\n"
    cin >> A;
    cout << "Please enter B\n"
    cin >> B;
    
    cout << A+B << "\n";
}

This is the solution.

Recommended Posts

python subprocess, for some reason it doesn't stdout.
A memo for when pip3 is installed with python2.7 for some reason
python [for myself]
[python] When pipenv install of WSL, python on Windows side is called for some reason
When OpenCV hconcat gives an error for some reason