Note that there are some explanatory texts on larger screens.

plurals
  1. POCout fixes bug in C++ program, but why?
    primarykey
    data
    text
    <p>I wrote a simple program to grab stock prices from yahoo finance. The loop that reads the data was truncating early (and ending about where the data from the website shows as opposed to the full download to the correct date for the excell file). So I put in a cout command in the loop to try to debug and voila, it worked correctly! </p> <p>So why does using the cout function alter the program function? Any ideas? Below is the code. (I found two related posts, but still cant figure it out, e.g. "Can cout alter variables somehow?" and "Weird Error in C++ Program: Removing Printout Breaks Program")</p> <pre><code>#include &lt;string&gt; #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;algorithm&gt; #include &lt;windows.h&gt; #include &lt;wininet.h&gt; using namespace std; int main() { HINTERNET hOpen, hURL; LPCWSTR NameProgram = L"Webreader"; // LPCWSTR == Long Pointer to Const Wide String LPCWSTR Website; char file[101]; int i; string filename; unsigned long read; filename = "data.txt"; ofstream myFile(filename); if (! myFile) { cout &lt; "Error opening file\n"; } if ( !(hOpen = InternetOpen(NameProgram, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 ))) { cerr &lt;&lt; "Error in opening internet" &lt;&lt; endl; return 0; } Website = L"http://ichart.finance.yahoo.com/table.csv?s=MSFT&amp;a=00&amp;b=1&amp;c=2009&amp;d=09&amp;e=22&amp;f=2010&amp;g=d&amp;ignore=.csv"; hURL = InternetOpenUrl( hOpen, Website, NULL, 0, 0, 0 ); //Need to open the URL InternetReadFile(hURL, file, 100, &amp;read); file[read] = '\0'; myFile &lt;&lt; file; while (read == 100) { InternetReadFile(hURL, file, 100, &amp;read); file[read] = '\0'; myFile &lt;&lt; file; cout &lt;&lt; file; //If I take this line out, the function terminates early. } myFile &lt;&lt; file; InternetCloseHandle(hURL); myFile.close(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload