Note that there are some explanatory texts on larger screens.

plurals
  1. POTerminating multi-line user input?
    primarykey
    data
    text
    <p>having a bit of a problem with a recent project. The goal here is to be able to input several lines of text containing a date in mm/dd/yyyy format followed by a whitespace and then a description of the event. I've accomplished actually allowing for multiple lines of text input (I think), but now I'm not exactly sure how to stop it. Is there a way to simply assume after a set amount of time, perhaps, that the user is done inputting? This problem is stressing me a lot as they never really addressed how to end user input, only how to accomplish multiple lines. We have to use the standard user input, not from text files which would be so much easier. Code below, please feel free to also tell me if I've made an unnoticed mistake elsewhere. I just need to be able to let the user finish the input so the program can re-order the events[] and output them. </p> <p><strong>calendar.cpp</strong></p> <pre><code>#include &lt;string&gt; #include &lt;sstream&gt; #include &lt;iostream&gt; #include &lt;vector&gt; using namespace std; #include "event.h" int main(){ // Maximum number of input lines const int MAX = 100; // Array of pointers to events Event *events[MAX]; // Number of currently used pointers in events int size = 0; int i = 0; // Vector containing each input line as a string vector&lt;string&gt; vec (100); char temps[100]; while(i &lt; MAX){ cin.getline(temps, 100); vec[i] = temps; i++; } for(int i = 0; i &lt; vec.size(); i ++){ int found = vec[i].find("/"); int tmo = atoi(vec[i].substr(0, found).c_str()); int sfound = vec[i].find("/", found + 1, 6); int tda = atoi(vec[i].substr(found + 1, sfound - (found + 1)).c_str()); int tye = atoi(vec[i].substr(sfound + 1, 4).c_str()); string tdes = vec[i].substr(sfound + 6, vec[i].length() - (sfound + 6)); events[size] = new Event(tmo, tda, tye, tdes); size++; } return 0; } </code></pre> <p><strong>event.cpp</strong></p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; using namespace std; #include "event.h" Event::Event(int mo, int da, int ye, string des){ month = mo; day = da; year = ye; desc = des; } Event::~Event(){ } void Event::print(){ cout &lt;&lt; month &lt;&lt; "/" &lt;&lt; day &lt;&lt; "/" &lt;&lt; year &lt;&lt; " " &lt;&lt; desc &lt;&lt; endl; } </code></pre> <p><strong>event.h</strong></p> <pre><code>#ifndef EVENT_H #define EVENT_H #include &lt;iostream&gt; #include &lt;string&gt; using namespace std; class Event{ public: Event(int mo, int da, int ye, string des); ~Event(); void print(); private: int month; int day; int year; string desc; }; #endif </code></pre> <p>Thank you in advance for your help.</p> <p>EDIT:</p> <p>Replacing my while() loop with:</p> <pre><code>while ((i &lt; MAX) &amp;&amp; (!cin.eof()) { cin.getline(temps, 100); vec[i] = temps; i++; } </code></pre> <p>Seemed promising but even after injecting an EOF marker with Ctrl + D (^D) as suggested, user input can still continue. Any reason why this might be happening?</p>
    singulars
    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.
    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