fork download
  1. #include <cstring>
  2. #include <string>
  3. #include <fstream>
  4. #include <algorithm>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. void transpositionCipher(char *text)
  10. {
  11. //var length is text's length
  12. int length = strlen(text);
  13. int key = 0, col = 0, row = 0, tempTextLength = length, numOfRows = 0, counter = 0,
  14. i = 0, j = 0;
  15. std::vector<std::vector<char>> tranArr;
  16.  
  17. for (key = 2; key < 7; key++) {
  18. while (tempTextLength % key != 0) {
  19. if (tempTextLength - 1 % key == 0) {
  20. tempTextLength--;
  21. }
  22. else tempTextLength++;
  23. }
  24.  
  25. numOfRows = tempTextLength / key;
  26.  
  27. //allocation
  28. tranArr.resize(numOfRows, std::vector<char>(key));
  29.  
  30. //fill the array
  31. for (i = 0; i < length; i++) {
  32. if (text[i] > 96 && text[i] < 123) {
  33. if (row == numOfRows) {
  34. col++;
  35. row = 0;
  36. }
  37. tranArr.at(row).at(col) = text[i];
  38. row++;
  39. }
  40. }
  41. // reset.
  42. row = 0; col = 0; tempTextLength = length; counter = 0;
  43. }
  44.  
  45. }
  46.  
  47. int main()
  48. {
  49. transpositionCipher("nalcxehwttdttfseeleedsoaxfeahl");
  50. }
  51.  
Runtime error #stdin #stdout #stderr 0s 3460KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 2) >= this->size() (which is 2)