Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First and foremost, your program is wrong. After the <code>while</code> loop ends, there is nothing more to read in the file (unless you <code>seekg()</code> back to the beginning), so the <code>std::getline()</code> call in the <code>for</code> loop body basically does nothing.</p> <p>A second problem is that concerns are not properly separated.</p> <p>Here is how I would have implemented this program:</p> <pre><code>struct line_data { Time t; double x; }; // This handles reading a single Time value. std::istream &amp; operator &gt;&gt; (std::istream &amp; is, Time &amp; t) { int hh, mm; if (is &gt;&gt; hh &gt;&gt; mm) { // Not happy with the following two lines, too Java-like. :-( t.setTimeHours(hh); t.setTimeMinutes(mm); } return is; } // This handles reading a single line of data. std::istream &amp; operator &gt;&gt; (std::istream &amp; is, line_data &amp; ld) { std::string s; if (std::getline(is, s)) { std::istringstream iss(s); // Ensure errors are propagated from iss to is. if (!(iss &gt;&gt; ld.t &gt;&gt; ld.x)) is.setstate(std::ios::failbit); } return is; }; // This handles processing a single line of data. struct line_manip // satisfies concept OutputIterator&lt;line_data&gt; { std::back_insert_iterator&lt;std::vector&lt;Time&gt;&gt; ti; std::back_insert_iterator&lt;std::vector&lt;double&gt;&gt; xi; line_manip(std::vector&lt;Time&gt; &amp; ts, std::vector&lt;double&gt; &amp; xs) : ti(std::back_inserter(ts)) , xi(std::back_inserter(xs)) { } line_manip &amp; operator = (const line_data &amp; ld) { ti = ld.t; xi = ld.x; return *this; } line_manip &amp; operator * () { return *this; } line_manip &amp; operator ++ () { return *this; } line_manip &amp; operator ++ (int) { return *this; } }; int main() { std::ifstream ifs("input.txt"); std::vector&lt;Time&gt; ts; std::vector&lt;double&gt; xs; std::copy(std::istream_iterator&lt;line_data&gt;(ifs), std::istream_iterator&lt;line_data&gt;(), line_manip(ts, xs)); // ... } </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.
    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