title: Runtime Error C++ USACO Training - " Run 1: Execution error: Your program had this runtime error:Illegal file open (gift1.out). "
body: <p>I've been attempting the USACO training page problems and have been receiving a runtime error that says &quot; Run 1: Execution error: Your program had this runtime error:
  Illegal file open (gift1.out). The program ran for 0.004 CPU
  seconds before the error. It used 1336 KB of memory. &quot;
  Here's the code</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  int main() {
    ofstream outfile (&quot;gift1.out&quot;);
    ofstream fout (&quot;gift1.out&quot;);
    ifstream fin (&quot;gift1.in&quot;);
    int numPeople;
    string currentGiver;
    string currentRec;
    int perPerson;
    int forGiver;
    fin &gt;&gt; numPeople;
    string name[numPeople];
    float amount[numPeople];
    float recNum[numPeople];
    int money[numPeople];
    for (int i = 0; i &lt; numPeople; i++){
      fin &gt;&gt; name[i];
      money[i] = 0;
    }
    for (int j = 0; j &lt; numPeople; j++){
      fin &gt;&gt; currentGiver;
      for (int i = 0; i &lt; numPeople; i++){
        if (currentGiver == name[i]){
          fin &gt;&gt; amount[i];
          fin &gt;&gt; recNum[i];
          perPerson = amount[i]/recNum[i];
          forGiver = amount[i] - (perPerson * recNum[i]);
          money[i] += forGiver;
          money[i] -= amount[i];
          for (int k = 0; k &lt; recNum[i]; k++){
            fin &gt;&gt; currentRec;
            for (int m = 0; m &lt; numPeople; m++){
              if (currentRec == name[m]){
                money[m] += (perPerson);
              }
            }
          }
        }
      }   
    }
    for (int i = 0; i &lt; numPeople; i++){
      fout &lt;&lt; name[i] &lt;&lt; &quot; &quot; &lt;&lt;money[i] &lt;&lt; endl;   
    }
    return 0;
  }
  </code></pre>