title: Caesar cipher program in c++
body: <p>I am trying to write a caesar cipher program in c++. I use four functions, one for choosing shift key , two for encryption and decryption and the last is for implement the caesar cipher, using an inputfile for reading the text and an ouput the encrypted or the decrypted text into the output file. I am trying to run the code and it is being crashed. I think the problem is somewhere inside Caesar function. But i don't know what exactly is. Any ideas?    This is my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  using namespace std;
  
  int chooseKey()
  {
      int key_number;
      cout &lt;&lt; "Give a number from 1-26: ";
      cin &gt;&gt; key_number;
  
      while(key_number&lt;1 || key_number&gt;26)
      {
          cout &lt;&lt; "Your number have to be from 1-26.Retry: ";
          cin &gt;&gt; key_number;  
  
      }
      return key_number;
  }
  
  void encryption(char *w, char *e, int key){
  
      char *ptemp = w;
  
  
      while(*ptemp){
  
          if(isalpha(*ptemp)){
  
          if(*ptemp&gt;='a'&amp;&amp;*ptemp&lt;='z')  
           {  
               *ptemp-='a';  
               *ptemp+=key;  
               *ptemp%=26;  
               *ptemp+='A';  
           }  
  
  
          }
          ptemp++;
      }
  
      w=e;
  
  }
  
  void decryption (char *e, char *w, int key){
      char *ptemp = e;
  
  
      while(*ptemp){
  
          if(isalpha(*ptemp))
          {
  
              if(*ptemp&gt;='A'&amp;&amp;*ptemp&lt;='Z')  
              {  
               *ptemp-='A';  
               *ptemp+=26-key;  
               *ptemp%=26;  
               *ptemp+='a';  
              }  
  
  
          }
          ptemp++;
      }
  
      e=w;
  
  }
  
  void Caesar (char *inputFile, char *outputFile, int key, int mode)
  {
       ifstream input;
       ofstream output;
       char buf, buf1;  
       input.open(inputFile);  
       output.open(outputFile);  
       buf=input.get();
       while(!input.eof())  
       {  
          if(mode == 1){
              encryption(&amp;buf, &amp;buf1, key);
          }else{
              decryption(&amp;buf1, &amp;buf, key);
          }
          output &lt;&lt; buf;
          buf=input.get();
       }
       input.close();  
       output.close();
  }
  
  int main(){
  
      int key, mode;
      key = chooseKey();
      cout &lt;&lt; "1 or 0: ";
      cin &gt;&gt; mode;
      Caesar("test.txt","coded.txt",key,mode);
      system("pause");
  }
  </code></pre>
  
  <hr>
  
  <p>Here are some details for my program.The template of my code is on top of my post. </p>
  
  <p>The parameter <em>w is the text in the beginning and the par e</em> is the decrypted text. The encryption is being made, if we add the shift key in the w* text and we get the *e. The reverse procedure makes the decryption function, i have the encrypted text(*e) and i want to take the the decrypted text(*w), subtracking the key shift which i take from chooseKey function. In the Caesar function, i use an input file which contain some data and i want to extract in the output file the encrypted or the decrypted text instead. I have already implement encryption and decryption functions and my problem is how to use them in the Caesar function.I don't take any desirable result, my program is being crashed. Please mention my mistakes.Any solution?</p>