Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>for( int i= 0; i &lt; line.length(); i++) acid = line[i]; </code></pre> <p>Say line contains "abcd", this loop is going to do:</p> <pre><code>acid = 'a'; acid = 'b'; acid = 'c'; acid = 'd'; </code></pre> <p>Only the last assignment has any lasting affect. If you need to actually get three characters from line into acid - you probably want to use <code>+=</code> to add characters into <code>acid</code>, rather than <code>=</code>. But, if you loop over all of line like this, you'll end up doing <code>acid = line;</code>. I assume you want something more like <code>acid = line.substr(0, 3)</code>?</p> <pre><code>for (int i = 0; i &lt; 3; i++) { acids = acid[i]; </code></pre> <p>This is going to crash. <code>acid</code> is definitely a single character string, and you're indexing into <code>acid[1]</code> and <code>acid[2]</code> on the 2nd and 3rd iterations. While you're learning C++, you should probably use <code>.at(i)</code> which will throw an exception when you attempt to use an invalid index - you can catch the exception and at least have some indication of the problem. As is, it's undefined behaviour.</p> <p>To use at, you need a <code>try</code> / <code>catch</code> block... the basic form is:</p> <pre><code>int main() try { ...your code in here... some_string.at(i); } catch (const std::exception&amp; e) { std::cerr &lt;&lt; "caught exception: " &lt;&lt; e.what() &lt;&lt; '\n'; } </code></pre> <p>More generally, try putting some <code>std::cout</code> statements throughout your code so you know what values your variables actually have... you would easily have seen that they weren't what you expected. Alternatively, use an interactive debugger and watch the affect of each statement's execution.</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