Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Declaring an array of strings is easy, the same as declaring an array of anything else.</p> <pre><code>const int MaxWords = 100; std::string piglatin[MaxWords]; </code></pre> <p>That's an array of 100 string objects, and the array is named <code>piglatin</code>. The strings start out empty. You can fill the array like this:</p> <pre><code>int numWords = 0; std::ifstream input("piglatin.txt"); std::string line; while (std::getline(input, line) &amp;&amp; numWords &lt; MaxWords) { piglatin[numWords] = line; ++numWords; } if (numWords == MaxWords) { std::cerr &lt;&lt; "Too many words" &lt;&lt; std::endl; } </code></pre> <hr> <p>I strongly recommend you <em>not</em> use an array. Use a container object, such as <a href="http://dinkumware.com/manuals/?manual=compleat&amp;page=vector.html#vector" rel="nofollow noreferrer"><code>std::vector</code></a> or <a href="http://dinkumware.com/manuals/?manual=compleat&amp;page=deque.html#deque" rel="nofollow noreferrer"><code>std::deque</code></a>, instead. That way, you can load the contents of the files without knowing in advance how big the files are. With the example declaration above, you need to make sure you don't have more than 100 entries in your file, and if there are fewer than 100, then you need to keep track of how many entries in your array are valid.</p> <pre><code>std::vector&lt;std::string&gt; piglatin; std::ifstream input("piglatin.txt"); std::string line; while (std::getline(input, line)) { piglatin.push_back(line); } </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