Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For context, you could have provided a link to <a href="https://stackoverflow.com/questions/1663615/translating-program">your previous question</a>, about storing two lists of words in different languages. There I provided an example of reading the contents of a text file into an array:</p> <pre><code>const int MaxWords = 100; std::string piglatin[MaxWords]; 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> <p>You can't have <strong><em>one</em></strong> parallel array. For something to be parallel, there must be at least two. For parallel arrays of words, you could use a declarations like this:</p> <pre><code>std::string piglatin[MaxWords]; std::string english[MaxWords]; </code></pre> <p>Then you have two options for filling the arrays from the file:</p> <ol> <li><p>Read an entire line, and the split the line into two words based on where the first space is:</p> <pre><code>while (std::getline(input, line) &amp;&amp; numWords &lt; MaxWords) { std::string::size_type space = line.find(' '); if (space == std::string::npos) std::cerr &lt;&lt; "Only one word" &lt;&lt; std::endl; piglatin[numWords] = line.substr(0, space); english[numWords] = line.substr(space + 1); ++numWords; } </code></pre></li> <li><p>Read one word at a time, and <em>assume</em> that each line has exactly two words on it. The <code>&gt;&gt;</code> operator will read a word at a time automatically. (If each line doesn't have exactly two words, then you'll have problems. Try it out to see how things go wrong. Really. Getting experience with a bug when you <em>know</em> what the cause is will help you in the future when you <em>don't</em> know what the cause is.)</p> <pre><code>while (input &amp;&amp; numWords &lt; MaxWords) { input &gt;&gt; piglatin[numWords]; input &gt;&gt; english[numWords]; ++numWords; } </code></pre></li> </ol> <p>Now, if you really one <em>one</em> array with <em>two elements</em>, then you need to define another data structure because an array can only have one "thing" in each element. Define something that can hold two strings at once:</p> <pre><code>struct word_pair { std::string piglatin; std::string english; }; </code></pre> <p>Then you'll have just one array:</p> <pre><code>word_pair words[MaxWords]; </code></pre> <p>You can fill it like this:</p> <pre><code>while (std::getline(input, line) &amp;&amp; numWords &lt; MaxWords) { std::string::size_type space = line.find(' '); if (space == std::string::npos) std::cerr &lt;&lt; "Only one word" &lt;&lt; std::endl; words[numWords].piglatin = line.substr(0, space); words[numWords].english = line.substr(space + 1); ++numWords; } </code></pre> <p>Notice how the code indexes into the <code>words</code> array to find the next <code>word_pair</code> object, and then it uses the <code>.</code> operator to get to the <code>piglatin</code> or <code>english</code> field as necessary.</p>
 

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