title: Why is it not printing in the right order?
body: <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  int output_1(string s, char c);
  int output_2(string s, char c);
  
  int main() {
  
      string str;
      char c;
  
      cout &lt;&lt; &quot;Enter a string: &quot;;
      getline(cin, str);
  
      cout &lt;&lt; &quot;Enter a character: &quot;;
      cin &gt;&gt; c;
  
      cout &lt;&lt; &quot;[1] the number of times the character appears: &quot; &lt;&lt; output_1(str, c) &lt;&lt; endl;
      
      cout &lt;&lt; &quot;[2] That character is found at Index/Indices: &quot; &lt;&lt; output_2(str, c) &lt;&lt; endl;
  
      return 0;
  }
  
  int output_1(string s, char c) {
  
      int count = 0;
  
      for (int x = 0; x &lt; s.length(); x++)
  
          if (s[x] == c)
              count++;
  
      return count;
  }
  
  int output_2(string s, char c) {
      
      for (int x = 0; x &lt; s.length(); x++) {
          if (c == s[x]){
              cout &lt;&lt; x &lt;&lt; &quot; &quot;;
          }
      }
  }
  </code></pre>
  <p>Why is the second output not in order and have an extra 0 at the end?</p>
  <p>Enter a string: test <br>
  Enter a character: t <br>
  [1] the number of times the character appears: 2<br>
  0 3 [2] That character is found at Index/Indices: 0</p>