title: Receiving garbage values while printing character array
body: <p>I'm writing a code to encrypt the entered text in Caesar Cipher but I'm getting a problem. While running my code my loop is not being terminated at null character. Code is as follows:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  void main()
  {
      char message[200], en_message[200];
  
      cout &lt;&lt; "Enter your message to encrypt: ";
      std::cin.getline(message,200);
  
      for ( int index = 0 ; message[index] != '\0' ; index++ )
      {
          if ( message[index] == 'A' )
              en_message[index] = 'X';
  
          else if ( message[index] == 'B' )
              en_message[index] = 'Y';
  
          else if ( message[index] == 'C' )
              en_message[index] = 'Z';
  
          else
              en_message[index] = message[index] - 3;
      }
  
          cout &lt;&lt; en_message;
  }
  </code></pre>
  
  <p>What I have tried:</p>
  
  <p>1) Using a loop to output array "en_message" with
   Using " en_message[index] != '\0' " and " en_message[index] != ' ' " as condition in for loop</p>
  
  <p>2) Using an if condition to break the loop.</p>
  
  <p>No matter what I try, I get this output! <a href="http://i.stack.imgur.com/xfNeC.jpg" rel="nofollow">1</a></p>
  
  <p>Any help would be appreciated. Thanks all in advance.</p>
  
  <p>EDIT:
  Okay, so now I'm getting another problem. I tried the code in G++ compiler on my university's lab computer and it worked but at home I'm receiving this error "Run Time check failure # 2. Stack around variable 'en_message' was corrupt." I'm using Visual Studio 2010 . What could that be? the modified code is:</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  void main()
  {
      char message[200], en_message[200];
      int index;
  
      cout &lt;&lt; "Enter your message to encrypt: ";
      cin &gt;&gt; index;
  
      for ( index = 0 ; index &lt; 200 ; index++ )
      {
          if ( message[index] == '\0' )
              break;
  
          if ( message[index] == 'A' )
              en_message[index] = 'X';
  
          else if ( message[index] == 'B' )
              en_message[index] = 'Y';
  
          else if ( message[index] == 'C' )
              en_message[index] = 'Z';
  
          else
              en_message[index] = message[index] - 3;
      }
      en_message[index] = '\0';
  
  
      cout &lt;&lt; en_message;
  }
  </code></pre>