Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, you don't seem to have any way of exiting the loop if you enter <code>yes</code>, it simply prints <code>test</code> and continues on its merry way, trying to read more data.</p> <p>If you mean that it's not printing <code>test</code> the first time you enter <code>yes</code> then you need to temporarily change:</p> <pre><code>getline(cin,input); </code></pre> <p>to:</p> <pre><code>getline (cin, input); cout &lt;&lt; "[" &lt;&lt; input &lt;&lt; "]" &lt;&lt; endl; </code></pre> <p>to try and find out what's actually <em>in</em> that buffer when you come to compare it.</p> <hr> <p>For what it's worth, this code (very similar to yours) works just fine:</p> <pre><code>#include &lt;iostream&gt; int main (void) { while (true) { int read = -1; if (read == -1) { std::cout &lt;&lt; "Connection lost, exit?" &lt;&lt; std::endl; std::string input; getline (std::cin, input); std::cout &lt;&lt; "[" &lt;&lt; input &lt;&lt; "]" &lt;&lt; std::endl; if (input == "yes") { std::cout &lt;&lt; "You entered 'yes'" &lt;&lt; std::endl; break; } } std::cout &lt;&lt; "Rest of loop" &lt;&lt; std::endl; } return 0; } </code></pre> <hr> <p>You should also be aware that you have a gaping hole in your logic. When you do a <code>strtok</code> on your buffer, it modifies the buffer and gives you a pointer into it (i.e., the actual buffer). It doesn't make a copy for you.</p> <p>If you then save away those pointers in <code>info</code> and pass this off to another thread, at the same time as you go back and read <em>more</em> information into that buffer then your main thread, and the thread you passed the information to, are going to clash. Badly.</p> <p>If you must use <code>strtok</code> to get the information, make sure you <code>strdup</code> it and pass the <em>copies</em> to your other thread (remembering to free them when done). Most C implementations will have a <code>strdup</code> (though it's not ISO standard). If yours doesn't, see <a href="https://stackoverflow.com/questions/252782/strdup-what-does-it-do-in-c/252802#252802">here</a>.</p> <p>There's a moderately good chance this hole may be causing your strange behaviour.</p> <p>The best way to fix this is to change the two lines:</p> <pre><code> info-&gt;_IP = p; info-&gt;_Port= p; </code></pre> <p>into:</p> <pre><code> info-&gt;_IP = strdup (p); info-&gt;_Port= strdup (p); </code></pre> <p>and remember to <code>free</code> both those memory allocations in your other thread when it's finished with them (normally I wouldn't advocate <code>malloc</code>-type operations for C++ but, since you're already using character buffers rather than strings, it seems the easiest solution).</p>
 

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