cin doesn't work along with freopen in wsl windows

128 Views Asked by At

I am running this simple cpp code in wsl and it outputs infinite times.

The reason being when I take input into the variable t, it simply doesn't take, and it still has the garbage value initiated to it.

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    int n;
    cin>>n;
    cout<<n;
}

int32_t main() {
    freopen("input.txt","r",stdin);
    int t;cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

I am using WSL on VSCode.

Input:

2 1 0

Output:

output screenshot

The cin works fine with normal console input, but doesn't work with freopen().

Why is this happening? Is this a problem with the freopen() function? Or is there something wrong in my WSL installation?

0

There are 0 best solutions below