Jun 7, 2016 at 10:20pm UTC
I want the program to output a permutation of the entire string p input. The problem is that when the string is bigger than the permu string it freezes.
thanks for any help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
using namespace std;
const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,' " ;
string create_permutation(unsigned seed)
{
srand(seed);
string permutation = ALPHABET;
// using built-in random generator:
random_shuffle(permutation.begin(), permutation.end());
return permutation;
}
int seed = 5;
string permu = create_permutation(seed);
// This works with input less than const ALPHABET string. but if it is bigger it freezes the program.
void fixed_Rotor()
{
char ALICE[30];
char BOB[30];
char option;
int seed = 0;
string p;
cout << "Enter seed to generate permutation: " ;
cin >> seed;
string permu = create_permutation(seed);
cout << "encipher 'E' / decipher 'D' / quite 'Q' " ;
cin >> option;
cin.ignore();
if (option == 'E' )
{
cout << "enter message to encipher" << endl;
getline(cin,p);
for (int i=0; i<p.size(); i++)
{
for (int j=0; j<permu.size();j++)
{
if (p[i] == permu[j])
{
ALICE[i] = permu[i];
}
}
}
for (int i=0; i<p.size(); i++)
{
cout << ALICE[i];
}
}
}
int main()
{
fixed_Rotor();
}
Last edited on Jun 8, 2016 at 9:52am UTC
Jun 7, 2016 at 10:24pm UTC
Consider the size of your character arrays (ALICE and BOB).
Jun 7, 2016 at 10:32pm UTC
I did change the size of the arrays but it outputs random characters that are not in the alphabet string.
Jun 8, 2016 at 1:53am UTC
could you show us a normal output and a buggy output ?
Jun 8, 2016 at 4:02am UTC
You've got a typo line 69 vs line 28
PS this is not the silver bullet though. :)
Last edited on Jun 8, 2016 at 4:12am UTC
Jun 8, 2016 at 9:52am UTC
This is the normal output :
Enter seed to generate permutation: 4
encipher 'E' / decipher 'D' / quite 'Q' E
enter message to encipher
HELLO
QGVXB // enciphered text.
Process returned 0 (0x0) execution time : 5.045 s
Press any key to continue.
buggy output: //When the message is bigger than permutation.
http://imgur.com/ZQIzWjZ
QGVXBLEHC SFWIJDUKP,OMYZNR'.TA // This is the permutation when the seed is 4.
EDIT: I think the problem is that the permutation string above is too small so the rest of the output is filled with random characters.
Last edited on Jun 8, 2016 at 12:17pm UTC
Jun 8, 2016 at 1:40pm UTC
As PrivateRyan said, your max characters that you can use is 30 (Line 30). If you exceed it, then you should get the error "string subscript out of range" Either validate the user input to ensure it does not exceed 30 characters, increase the number of characters in the array, or use string.
Jun 8, 2016 at 6:49pm UTC
yeah you guys are right. The input string exceeds the size of the perm string, which is only 30 characters.