Program stops when Input message is too big

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
Consider the size of your character arrays (ALICE and BOB).
I did change the size of the arrays but it outputs random characters that are not in the alphabet string.
could you show us a normal output and a buggy output?
closed account (48T7M4Gy)
You've got a typo line 69 vs line 28

PS this is not the silver bullet though. :)
Last edited on
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
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.
yeah you guys are right. The input string exceeds the size of the perm string, which is only 30 characters.
Topic archived. No new replies allowed.