Note that there are some explanatory texts on larger screens.

plurals
  1. POInitializing a Vector of Objects from a .txt file
    primarykey
    data
    text
    <pre><code>#include&lt;iostream&gt; #include&lt;vector&gt; #include&lt;fstream&gt; #include "stock.h" int main(){ double balance =0, tempPrice=0; string tempStr; vector &lt; Stock &gt; portfolio; typedef vector&lt;Stock&gt;::iterator StockIt; ifstream fileIn( "Results.txt" ); for(StockIt i = portfolio.begin(); i != portfolio.end(); i++) { while ( !fileIn.eof( )) { getline(fileIn,tempStr); i-&gt;setSymbol(tempStr); fileIn &gt;&gt; tempPrice; i-&gt;setPrice(tempPrice); getline(fileIn,tempStr); i-&gt;setDate(tempStr); } fileIn.close(); } for(StockIt i = portfolio.begin(); i != portfolio.end(); i++){ cout&lt;&lt;i-&gt;getSymbol() &lt;&lt;endl; cout&lt;&lt;i-&gt;getPrice() &lt;&lt;endl; cout&lt;&lt;i-&gt;getDate() &lt;&lt;endl; } return 0; </code></pre> <p>}</p> <p>Sample text file, Results.txt:</p> <pre><code>GOOG 569.964 11/17/2010 MSFT 29.62 11/17/2010 YHOO 15.38 11/17/2010 AAPL 199.92 11/17/2010 </code></pre> <p>Now obviously, I want this program to create a vector of Stock Objects which has the appropriate set/get functionality for object: <code>Stock(string, double, string)</code>.</p> <p>Once that is done, I want to print out each individual member of each Object in the vector. </p> <p>One thing that boggles my mind about <code>fstream</code>, is how can it decipher spaces and end of lines, and intelligently read strings/ints/doubles and place them into the appropriate data type? Maybe it can't...and I have to add an entirely new functionality? </p> <p>now it would seem that I'm not actually creating a new object for each iteration of the loop? I <em>think</em> would need to do something along the lines of:</p> <p><code>portfolio.push_back(new Stock(string, double, string));</code>? I'm just not entirely sure how to get to that point.</p> <p>Also, this code should be interchangeable with <code>std::list</code> as well as <code>std::vector</code>. This program compiles without error, however, there is zero output.</p>
    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.
 

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