std::bad_alloc error in while loop
<p>Noob here. I know I'm not supposed to post homework, but I have no idea what could possibly be causing this issue. For some reason this while loop throws a std::bad_alloc exception just before it exits (ie when it reads end of file). It's only using 500 MB of RAM on a system with 8GB, so I'm pretty sure it's not overuse of memory. If someone could help, that would be great, because I've been searching for this issue for about four hours now and can't find a fix. It's probably something stupid, but I can't figure it out.</p>

<p>Here's the code:</p>

<pre><code>temperatures_in &gt;&gt; low &gt;&gt; high;
tempMin=low;
tempMax=high;
getLocation(temperatures_in, location);

while(!temperatures_in.eof()){
    temperatures_out &lt;&lt; left &lt;&lt; setw(20) &lt;&lt; location;
    temperatures_out &lt;&lt; right &lt;&lt; fixed &lt;&lt; showpoint &lt;&lt; setprecision(2) &lt;&lt; setw(6) &lt;&lt; low &lt;&lt; setw(6) &lt;&lt; high &lt;&lt; endl;
    temperatures_in &gt;&gt; low &gt;&gt; high;
    getLocation(temperatures_in, location);
    if(low&lt;tempMin){
        tempMin=low;
    }
    if(high&gt;tempMax){
        tempMax=high;
    }
    cout &lt;&lt; low &lt;&lt; " " &lt;&lt; tempMin &lt;&lt; " " &lt;&lt; high &lt;&lt; " " &lt;&lt; tempMax &lt;&lt; " " &lt;&lt; location;
}
</code></pre>

<p>Also, when I ran a debugger, it showed the exception somehow being thrown at line -1 (is that even possible?), even though I've tested outputs inside and outside the loop and it will execute code before the end of the loop, but not after.</p>

<p>Edit: Here's the getLocation() function:</p>

<pre><code>void getLocation(ifstream &amp;temperatures_in, string &amp;location){
    char a;
    temperatures_in.get(a);
    while(a==' '){
        temperatures_in.get(a);
    }
    location="";
    while(a!='\n'){
        location+=a;
        temperatures_in.get(a);
    }
}
</code></pre>
