-3

If my code just simple like:

#include <bits/stdc++.h>
using namespace std;
int main(){
    cout<<"hi";
    return 0;
}

then it can write hi in my .out file. But if my code just become more complex like:

#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
const int INF=1000000000;
struct Edge{
    int u,v,w;
    void inp(){
        cin>>u>>v>>w;
    }
    void out(){
        cout<<u<<' '<<v<<' '<<w<<'\n';
    }
};
int n,m;
Edge ed[5005];
int D[2505];
void bellman(){
    FOR(i,0,n-1) D[i]=-INF;
    D[s]=0;
    FOR(t,1,n-1) FOR(i,1,m){
        int u=ed[i].u,v=ed[i].v,w=ed[i].w;
        if (D[u]!=-INF&&D[v]<D[u]+w) D[v]=D[u]+w;
    }
    FOR(t,1,n) FOR(i,1,m){
        int u=ed[i].u,v=ed[i].v,w=ed[i].w;
        if (D[u]!=-INF&&D[v]<D[u]+w) D[v]=INF;
    }
}
int main(){
    cin>>n>>m;
    FOR(i,1,m) ed[i].inp();
    FOR(i,1,m) ed[i].out();
    bellman();
    cout<<D[n];
    return 0;
}

it just show

[Finished in 2.9s]

without write any output in my file.

this is my sublime-build file:

{
    "cmd": ["g++.exe", "-std=c++17", "${file}",
            "-o", "${file_base_name}.exe",
            "&&", "${file_base_name}.exe<input.inp>output.out"],
    "shell":true,
    "working_dir":"$file_path",
    "selector":"source.cpp"
}

I tried to reset, and I have no idea about this thing.

Yuu Chan
  • 5
  • 3
  • 6
    [Do not use bits/stdc.h](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Issylin Aug 06 '23 at 17:27
  • 3
    Please show the code that isn't working rather than the code that is – Alan Birtles Aug 06 '23 at 17:38
  • If you want to output both into a file and into a terminal, use [`tee`](https://man7.org/linux/man-pages/man1/tee.1.html). – Evg Aug 06 '23 at 19:19
  • What's the input to your program? I'm guessing something is going out of bounds of your arrays and crashing. Have you tried using a debugger? You should check that your reads from `cin` are successful before using the values. Avoid using macros like `FOR` they just make your code harder to read – Alan Birtles Aug 07 '23 at 06:28

1 Answers1

0

Based on the provided information, it seems like the issue might be related to how the input and output redirection is handled in your sublime-build file. When you run your C++ program with input and output redirection, it might be waiting for the input.inp file to be present before it executes the program and writes the output to output.out.

I don't know if this will fix this but you could throw that redirection part away:

{
    "cmd": ["g++.exe", "-std=c++17", "${file}", "-o", "${file_base_name}.exe", "&&", "${file_base_name}.exe"],
    "shell": true,
    "working_dir": "$file_path",
    "selector": "source.cpp"
}

The cpp file would need those:

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    // Redirect input from input.inp
    ifstream cin("input.inp");
    if (!cin) {
        cerr << "Error opening input file!" << endl;
        return 1;
    }

    // Redirect output to output.out
    ofstream cout("output.out");
    if (!cout) {
        cerr << "Error opening output file!" << endl;
        return 1;
    }

    // Your main code here
    cout << "hi" << endl;

    return 0;
}
Mr.Oz
  • 51
  • 4