Note that there are some explanatory texts on larger screens.

plurals
  1. POcin.get() issue with empty line
    text
    copied!<p>When I run the following code and insert a new line (press enter) when prompted for the golf structure, the second call to the function doesn't request input and finishes as if I've pressed enter again.</p> <p>I've read up on : cin.get() , cin.clear() , cin.ignore(...) but nothing seems to help.</p> <p>I'm pretty sure that it has nothing to do with multiple .cpp files and the header but I'm putting the code as is.</p> <p>I'm using Visual Studio C++ 2010 - Express.</p> <p>Thanks in advance for your help!</p> <p>header file : golf.h</p> <pre><code>#ifndef GOLF_H #define GOLF_H const int Len = 40; struct golf{ char fullname[Len]; int handicap; }; int setgolf(golf &amp; g ); void showgolf(const golf &amp; g ); #endif </code></pre> <p>golf.cpp</p> <pre><code>#include "stdafx.h" #include "golf.h" #include &lt;iostream&gt; #include &lt;string&gt; using namespace std; int setgolf(golf &amp; g ){ cout &lt;&lt; "Enter a name for the golf structure:" &lt;&lt; endl; if (cin.get(g.fullname,Len)) { cin.get(); // deals with the '\n' incase the user inputs a valid string return 1; } else{ return 0; } } void showgolf(const golf &amp; g ){ cout &lt;&lt; "this golf structure contains the following information:" &lt;&lt; endl; cout &lt;&lt; "name: " &lt;&lt; g.fullname &lt;&lt; endl ; cout &lt;&lt; "handicap: " &lt;&lt; g.handicap &lt;&lt; endl ; } </code></pre> <p>main ()</p> <pre><code>#include "stdafx.h" #include "golf.h" #include &lt;iostream&gt; #include &lt;string&gt; using namespace std; int main() { golf myGolf; // check of int setgolf(golf &amp; g ); int answ = setgolf(myGolf); //try to input empty string showgolf(myGolf); cout &lt;&lt; "the number returned :" &lt;&lt; answ &lt;&lt; endl ; answ = setgolf(myGolf); // normal string showgolf(myGolf); cout &lt;&lt; "the number returned :" &lt;&lt; answ &lt;&lt; endl ; return 0; } </code></pre>
 

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