Guest User

Untitled

a guest
Feb 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cctype>
  5.  
  6. using namespace std;
  7.  
  8. const int alphabet = 26;
  9.  
  10. int shift(int&);
  11. void encryption(int&, char[alphabet], char [30]);
  12. void decryption(int&, char[alphabet], char [30]);
  13.  
  14. int main()
  15. {
  16.     char value;
  17.     char letters[alphabet] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  18.     char input[30];
  19.     int shiftamount;
  20.  
  21.     cout << "Which action do you want to perform?" << endl;
  22.     cout << "(1) Encryption" << endl;
  23.     cout << "(2) Decryption" << endl;
  24.  
  25.     while(value != 1 && value != 2)
  26.     {
  27.         cin >> value;
  28.  
  29.         switch(value)
  30.         {
  31.             case'1':
  32.             {
  33.                 shift(shiftamount);
  34.                 encryption(shiftamount, letters, input);
  35.             }
  36.  
  37.             case'2':
  38.             {
  39.                 shift(shiftamount);
  40.                 decryption(shiftamount, letters,input);
  41.             }
  42.             default:
  43.                 cout << "Error: Wrong value chosen. Try again." << endl;
  44.         }
  45.     }
  46.  
  47.     return 0;
  48. }
  49.  
  50. int shift(int& shiftamount)
  51. {
  52.     cout << "How many letters to the right do you want the alphabet to shift?" << endl;
  53.     cin >> shiftamount;
  54.  
  55.     return 0;
  56. }
  57.  
  58. void encryption(int& shiftamount, char letters[], char input[])
  59. {
  60.     cout << "Input the message you wish to encrypt:" << endl;
  61.     cin.getline(input, 30);
  62.  
  63.     char cipher[30];
  64.  
  65.     for(int i = 0; i < alphabet; i++)
  66.     {
  67.         letters[i] = toupper(letters[i]);
  68.         int x = input[i] - 'A';
  69.         cipher[i] = x + shiftamount;
  70.         cout << cipher[i];
  71.     }
  72.  
  73.     return;
  74. }
  75.  
  76. void decryption(int& shiftamount, char letters[], char input[])
  77. {
  78.     cout << "Input the message you wish to decrypt:" << endl;
  79.     cin.getline(input, 30);
  80.  
  81.     char cipher[30];
  82.  
  83.     for(int i = 0; i < alphabet; i++)
  84.     {
  85.         int x = input[i] - 'a';
  86.         cipher[i] = x + shiftamount;
  87.         cout << cipher[i];
  88.     }
  89.  
  90.     return;
  91. }
Add Comment
Please, Sign In to add comment