Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>numberOfStrings</code> variable is only updated once after the <code>for</code> loop has finished. You can also simplify this without the need to specify a large number of lines to read by checking the return value of <code>getline</code> for failure. If you try to read past the end of file is <code>getline</code> will return false.</p> <pre><code>numberOfStrings = 0; str = new std::string[1000000]; std::ifstream myfile("Read.txt"); if (myfile.is_open()) { std::string line; while(getline(myfile, str[numberOfStrings])) numberOfStrings++; myfile.close(); } </code></pre> <p>You can simplify this even further by using <a href="http://en.cppreference.com/w/cpp/container/vector" rel="nofollow"><code>std::vector</code></a>. To expand on the example provided in your answer <code>StringList</code> might look something like below.</p> <blockquote> <p>StringList.h</p> </blockquote> <pre><code>#include &lt;vector&gt; #include &lt;string&gt; class StringList { public: StringList(); void PrintWords(); private: size_t numberOfLines; std::vector&lt;std::string&gt; str; }; </code></pre> <blockquote> <p>StringList.cpp to read in single line into each string</p> </blockquote> <pre><code>#include "StringList.h" #include &lt;fstream&gt; StringList::StringList() { std::ifstream myfile("Read.txt"); if (myfile.is_open()) { std::string line; while(getline(myfile, line)) { lines_.push_back(line); } myfile.close(); } numberOfLines = str.size(); } </code></pre> <blockquote> <p>StringList.cpp to read in single word into each string using <code>std::istream_itertor</code> and <code>std::copy</code></p> </blockquote> <pre><code>#include "StringList.h" #include &lt;fstream&gt; #include &lt;istream&gt; #include &lt;algorithm&gt; // std::copy #include &lt;iterator&gt; // istream_iterator StringList::StringList() { std::ifstream myfile("Read.txt"); if (myfile.is_open()) { std::copy( istream_iterator&lt;string&gt;(myfile), istream_iterator&lt;string&gt;(), back_inserter(str)); } numberOfLines = str.size(); } </code></pre> <blockquote> <p>Some other function to print the words</p> </blockquote> <pre><code>StringList::PrintWords() { for(size_t i = 0; i &lt; numberOfLines; ++i) { std::cout &lt;&lt; str[i] &lt;&lt; std::endl; } } </code></pre> <hr> <p><sub>I also recommend avoiding using <code>using namespace std</code> in your code. It pulls everything from <code>std</code> into the current scope (typically the global namespace) and can cause conflicts with identifiers.</sub></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.
    1. VO
      singulars
      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