title: need help on debugging my c++ programming (text ascii manupulation)
body: <p>I recently made a C++ program which encrypts texts based-on the vigenere cipher technique. </p>
  
  <p>The encryption part works fine I think, but the decryption function doesn't seem to output the correct answer in some cases. I was hoping if anyone could have a look at code and tell me what's wrong with it.</p>
  
  <p><strong>CMD dialog:</strong></p>
  
  <p>Enter the key for encrytion:
  <strong>magic</strong></p>
  
  <p>Enter message No. 1:
  <strong>I love C programming</strong></p>
  
  <p>Message encrypted:
  <strong>u rwxq i rdomzcymovi</strong></p>
  
  <p>Decrypted:
  <strong>i love c pXogramming</strong></p>
  
  <p>Somehow it outputted "X" instead of "r" in this case.............</p>
  
  <p>Here are the codes:</p>
  
  <p>Secret.h:</p>
  
  <pre><code>#ifndef CPP_TUTORIALS_SECRET_H
  #define CPP_TUTORIALS_SECRET_H
  #include &lt;iostream&gt;
  using namespace std;
  
  class Secret {
  
  private:
      string message;
      string key;
      bool toEncrypt;
      bool toDecrypt;
  
  public:
      bool isToDecrypt() const {
          return toDecrypt;
      }
  
      Secret(const string &amp;message = "", const string &amp;key = "", bool toEncrypt = false);
  
      ~Secret();
  
      void setToDecrypt(bool toDecrypt);
  
      void encrypt();
  
      void decrypt();
  
      void display();
  
  };
  
  #endif //CPP_TUTORIALS_SECRET_H
  </code></pre>
  
  <p>Secret.cpp</p>
  
  <pre><code>#include "Secret.h"
  
  Secret::Secret(const string &amp;message, const string &amp;key, bool toEncrypt) {
      Secret::message = message;
      Secret::key = key;
      Secret::toEncrypt = toEncrypt;
  }
  
  Secret::~Secret(){
  
  }
  
  void Secret::setToDecrypt(bool toDecrypt) {
      Secret::toDecrypt = toDecrypt;
  }
  
  void Secret::display() {
      cout &lt;&lt; message &lt;&lt; endl;
  }
  
  void Secret::encrypt() {
      if(toEncrypt) {
          int keyAscii[key.length()];
          int count = 0;
  
          for(unsigned int i = 0; i &lt; key.length(); i++) {
              keyAscii[i] = key.at(i);
          }
  
          for(unsigned int i = 0; i &lt; message.length(); i++) {
              if (message.at(i) &gt; 64 &amp;&amp; message.at(i) &lt; 91) {
                  message.at(i) = (char)((message.at(i) - 65 + (keyAscii[count] - 97)) % 26 + 97);
              }
              else if (message.at(i) &gt; 96 &amp;&amp; message.at(i) &lt; 123) {
                  message.at(i) = (char)((message.at(i) - 97 + (keyAscii[count] - 97)) % 26 + 97);
              }
              else{
                  message.at(i) = message.at(i);
              }
              count++;
              if(count == key.length()) {
                  count = 0;
              }
          }
      }
  }
  
  void Secret::decrypt() {
      if(toDecrypt) {
          int keyAscii[key.length()];
          int count = 0;
  
          for(unsigned int i = 0; i &lt; key.length(); i++) {
              keyAscii[i] = key.at(i);
          }
  
          for(unsigned int i = 0; i &lt; message.length(); i++) {
              if (message.at(i) &gt; 96 &amp;&amp; message.at(i) &lt; 123) {
                  message.at(i) = (char)((message.at(i) - 97) % 26 - (keyAscii[count] - 97) + 97);
              }
              else {
                  message.at(i) = message.at(i);
              }
              count++;
              if (count == key.length()) {
                  count = 0;
              }
          }
      }
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;limits&gt;
  #include "Secret.h"
  
  void calcMsgAmount(int &amp;pArraySize);
  void inputKey(string &amp;key);
  void encryptMsg(Secret &amp;secret, string &amp;msg, const string &amp;key, bool toEncrypt, int index);
  
  int main() {
      int arraySize;
      string key;
      string msg;
  
      calcMsgAmount(arraySize);
      inputKey(key);
  
      Secret secrets[arraySize];
  
      for(int i = 0; i &lt; arraySize; i++){
          encryptMsg(secrets[i], msg, key, true, i);
      }
  
      cout &lt;&lt; endl &lt;&lt; "Message encrypted: " &lt;&lt; endl;
      for(Secret i: secrets){
          i.display();
          i.setToDecrypt(true);
          if(i.isToDecrypt()){
              i.decrypt();
              cout &lt;&lt; endl &lt;&lt; "Decrypted: " &lt;&lt; endl;
              i.display();
          }
          cout &lt;&lt; endl &lt;&lt; endl;
      }
  
      return 0;
  }
  
  void calcMsgAmount(int &amp;pArraySize) {
      cout &lt;&lt; "Enter the amount of messages you want to input: " &lt;&lt; endl;
      while(!(cin &gt;&gt; pArraySize)){
          cout &lt;&lt; endl &lt;&lt; "There's something really wrong with your input, please enter again." &lt;&lt; endl;
          cout &lt;&lt; "Enter the amount of messages you want to input: " &lt;&lt; endl;
          cin.clear();
          cin.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n');
      }
  }
  
  void inputKey(string &amp;key){
      cout &lt;&lt; "Enter the key for encrytion: " &lt;&lt; endl;
      cin.ignore();
      getline(cin, key);
  }
  
  void encryptMsg(Secret &amp;secret, string &amp;msg, const string &amp;key, bool toEncrypt, int index){
      cout &lt;&lt; "Enter message No. " &lt;&lt; index + 1 &lt;&lt; ": " &lt;&lt; endl;
      getline(cin, msg);
      secret = Secret(msg, key, toEncrypt);
      secret.encrypt();
  }
  </code></pre>
  
  <p>Many thanks</p>