Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are not experiencing a runtime error because your array stores more than it should. It might be, that when you input the name, it contains spaces. This would make <code>cin &gt;&gt; score;</code> read only the first character, leaving the rest inside the input buffer.</p> <p>Here's the result I run your code:</p> <pre><code>[wolf@Targaryen]:~$ r Enter the name for score # 1 :Alex Enter the score for score # 1 :100 Enter the name for score # 2 :Bob Enter the score for score # 2 :99 Enter the name for score # 3 :Charlie Enter the score for score # 3 :98 Enter the name for score # 4 :Douglas Enter the score for score # 4 :97 Enter the name for score # 5 :Evin Enter the score for score # 5 :96 100 99 98 97 96 [wolf@Targaryen]:~$ </code></pre> <p>However, your code does have an issue. The loop <code>for ( int j=i; j&lt;= i ;j++ )</code> only executes once, but that won't cause any error.</p> <p>You should read your name input using:</p> <pre><code>getline(cin, name); </code></pre> <p>Then you should clear out the input buffer by sinking any trash left in it to an unused variable.</p> <p>I think you may change your code like this:</p> <pre><code>#include &lt;iostream&gt; #include &lt;cstdio&gt; using namespace std; int main() { const int num = 5; string t[num], name; int m[num], score; for(int i=0; i &lt; num; i++) { cout &lt;&lt; "Enter the name for score # " &lt;&lt; i+1 &lt;&lt; " :"; getline(cin, name); t[i] = name; cout &lt;&lt; "Enter the score for score # " &lt;&lt; i+1 &lt;&lt; " :"; cin &gt;&gt; score; m[i] = score; getline(cin, name); // This line just clear out the buffer. "name" used as a trash } for(int i=0; i &lt; num; i++) cout &lt;&lt; t[i] &lt;&lt; ": " &lt;&lt; m[i] &lt;&lt; endl; } </code></pre>
    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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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