Arrays ('z' not declared in this scope) Error

I'm working with arrays now for BitCrypt, and for some reason, even while including everything I needed according to recently read threads, this code returns ('x' was not declared in this scope) when compiling with Code::Blocks MinGW. What's going on? I included everything and defined it correctly, why not defined? Code:

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
#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>

using namespace std;

int main()

{
    int ctr1;
    int ctr2;
    string output;
    char cipalp [25] = {z,y,x,w,v,u,t,s,r,q,p,o,n,m,l,k,j,i,h,g,f,e,d,c,b,a};
    char regalp [25] = {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};
    string input;
    getline(cin, input);

    int ctr = input.length() + 1; //it includes the null at the end

    char *arr = new char[ctr]; // make a new array (dynamic size)

    for(int i=0; i < ctr; i++)
    {
        arr[i] = input[i];
    }
    for (ctr1 = 0; ctr1 < arr.length(); ctr1++) {
        for (ctr2 = 0; ctr2 < arr.length(); ctr2++) {
            if (arr[ctr1] == regalp[ctr2]) {
                output = output + cipalp[ctr2]
            }
        }
    }
    delete arr; //free the memory
    return 0;
}

when you define char's you must include the apostrophe around the character or C++ thinks its a a variable. so:

1
2
char cipalp [25] = {'z', 'y'...'a'};
Also, I'd swear there are 26 letters in the alphabet. Your array seems to be declared one element short.
Don't arrays start with 0? That's why I put 25. Because 1+25=26, where one is the zero element (a in regalp).
25 places in the array, from 0-24 ;)

26 = 0-25.
This is the reversal cipher code I came up with. I fixed about 20 things, but all it does is compile and let you input. No output ciphertext. Why is this?:

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
#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>

using namespace std;

int main()

{
    int ctr; // input array's length
    int ctr1; // input counter
    int ctr2; // output counter
    string output; // output string to display. Arrays for ciphering are as follows:
    char cipalp [26] = {'z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'};
    char regalp [26] = {'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'};
    string input; // input string initilized
    getline(cin, input); // Get user input.
    ctr = input.length() + 1; //it includes the null at the end
    char *arr = new char[ctr]; // make a new array (dynamic size)
    for(int i=0; i < ctr; i++) // FOR loop to create array from input variable.
    {
        arr[i] = input[i];
    }
    ctr = input.length();
    for (ctr1 = 0; ctr1 < ctr2; ctr1++) { // Add 1 to input variable.
        if (ctr1 < ctr2) {
            for (ctr2 = 0; ctr2 < input.length(); ctr2++) {
                if (arr[ctr1] == regalp[ctr2]) {
                    output = output + cipalp[ctr2]; // Add the cipherchar to the output variable.
                }
            }
        }
    }
    cout << output;
    delete arr; //free the memory
    return 0;
}


This doesn't work either:

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
#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cmath>

using namespace std;

int main()

{
    int ctr;
    int ctr1; // input counter
    int ctr2; // cipher counter
    string output; // output string to display. Arrays for ciphering are as follows:
    char cipalp [26] = {'z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'};
    char regalp [26] = {'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'};
    string input; // input string initilized
    getline(cin, input); // Get user input.
    ctr = input.length() + 1; //it includes the null at the end
    char *arr = new char[ctr]; // make a new array (dynamic size)
    for(int i=0; i < ctr; i++) // FOR loop to create array from input variable.
    {
        arr[i] = input[i];
    }
    ctr2 = 0;
    for (ctr1 = 0; input.length() < ctr1; ctr1++) {
        ctr2++;
        if (arr[ctr1] = regalp[ctr2]) {
            output = output + cipalp[ctr2];
        }
    }
    cout << output;
    delete arr;
    return 0;
}
Last edited on
In line 29, I believe you mean == rather than =.

In all cases where you use new[], you should use delete[] -- not even sure why you're using dynamic memory here. Use a string or vector.

I'm not sure if the following is quite how you meant to do things, but perhaps you can take something from it.

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
#include <iostream>
#include <string>

namespace MySimpleSubstitutionAlgorithm  
{
    namespace {
        const char original[] = 
            "abcdefghijklmnopqrstuvwxyz"
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            "0123456789.?!" ;
        const char encoding[] =  
            "zyxwvutsrqponmlkjihgfedcba"
            "ZYXWVUTSRQPONMLKJIHGFEDCBA"
            "9876543210!.?";
    }

    std::string encode(const std::string & toEncode)
    {	
        std::string text = toEncode ;

        for ( unsigned i = 0;  i < text.length(); ++i )
            for ( unsigned j = 0 ;  j < sizeof(original)-1 ; ++j )
                if ( text[i] == original[j] )
                {
                    text[i] = encoding[j] ;
                    break ;
                }

        return text ;
    }
}


namespace SSA = MySimpleSubstitutionAlgorithm ;

int main()

{
    std::string input ;
    std::getline(std::cin, input) ;

    std::cout << SSA::encode(input) ;
}
Last edited on
Topic archived. No new replies allowed.