arrayOutput function and possibly arrayFill function not working

When I selected option 2 from the menu, it only displays "The new version is". It does not display the multiArray arrays.

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

class functions
{
    public:
        char alphabet[26];
        string sentance;

        void input ()
        {
            cout << endl;
            cout << "Write a sentence: ";
            getline(cin,sentance);

        }

        void arrayFill (int x, vector <char> tempArray)
        {
            int counter=0;

            for (int j=x;j<sentance.length();j+=4)
            {
                tempArray.push_back(1);
                tempArray[counter]=sentance[j];
                counter++;
            }

        }

        void arrayOutput (vector <char> tempArray)
        {
            for (int j=0;j<tempArray.size();j++)
            {
                cout << tempArray[j];
            }
        }

        void subCipher ()
        {
            cout << endl << "The new version is ";

            for (int i=0;i<sentance.length();i++)
            {
                if (sentance[i]==' ')
                    {
                        cout << " ";
                    }

                for (int j=0;j<26;j++)
                {
                    if (alphabet[j]==alphabet[23] && sentance[i]==alphabet[23])
                    {
                        cout << alphabet[0];
                    }

                    else if (alphabet[j]==alphabet[24] && sentance[i]==alphabet[24])
                    {
                        cout << alphabet[1];
                    }

                    else if (alphabet[j]==alphabet[25] && sentance[i]==alphabet[25])
                    {
                        cout << alphabet[2];
                    }

                    else if (alphabet[j]==sentance[i])
                    {
                        cout << alphabet[j+3];
                    }
                }
            }
            cout << endl << endl;
        }

        void multiArrayCipher()
        {
            vector <char> multiArrayOne;
            vector <char> multiArrayTwo;
            vector <char> multiArrayThree;
            vector <char> multiArrayFour;

            arrayFill(0,multiArrayOne);
            arrayFill(1,multiArrayTwo);
            arrayFill(2,multiArrayThree);
            arrayFill(3,multiArrayFour);

            cout << endl << "The new version is ";

            arrayOutput(multiArrayOne);
            arrayOutput(multiArrayTwo);
            arrayOutput(multiArrayThree);
            arrayOutput(multiArrayFour);

            cout << multiArrayOne[0] << endl << endl;
        }
};

int main()
{
    functions program;
    int input;

    ifstream file;
    file.open("alphabet.txt");

    for (int j=0; j<26;j++)
    {
        file >> program.alphabet[j];
    }

    while (input!=5)
    {
        cout << "1. Substitution cipher" << endl;
        cout << "2. Multi-array cipher" << endl;
        cout << "3. Transposition cipher" << endl;
        cout << "4. Code breaker" << endl;
        cout << "5. Exit" << endl << endl;
        cout << "Enter the number of the option you want to proceed with: ";
        cin >> input;
        cin.ignore();

        switch (input)
        {
            case 1:
                program.input();
                program.subCipher();
                break;

            case 2:
                program.input();
                program.multiArrayCipher();
                break;


        }

    }


    return 0;
}
Hello Shezshade,

When I tested the program I found the in the "arrayFill" function it would add to the temp vector, but not back to the original vector sent to the function. In the "arrayFill" function definition after <char> I added "&" to pass the variable by reference and that solved the problem of filling the "multiArray*" variables.

With your way all the "multiArray*" had a size of zero so there was nothing to print.

I have not tested out option 1 yet because I wanted to mention this and other points.

In main line 107 defines input, but gives it no value. This was a compile error for me when the variable is used at line 117. Although the garbage value in the variable was not == 0 it may not work the way that you want.

It is ALWAYS a good practice and programming to initialize your variables. If your compiler is using the C++11 standards or after the easiest way to initialize variables is with empty {}s, e.g., int num{};. This will initialize the variable to 0 (zero) or 0.0 for a double. A "char" will be initialized to "\0". "std::string"s are empty to start with and do not need to be initialized. Should you need to you can put a number between the {}s.You can also Initialize an array, e.g., int aNumbers[10]{};. This will initialize all elements of the array to 0 (zero). A use of
int aNumbers[10]{ 1 }; will initial the first element to "1" and the rest of the array to "0".. Following the "1" with ", 2 ..." will initialize the array with the numbers that you have used up to the entire array.

Although you are just getting started the switch should have a "case 5:" and a default at the end.

In your class you put "alphabet" and "sentence" in the public section. This gives the entire program access to these variables thus defeating the point of the class and the private section. You could remove lines 9, 10 and 11 along with the closing brace of the class and the program would still work. In main you would have to remove the "program." for everything to work.

A point of the class is to make the variables and sometimes functions private to keep them from the rest of the program. This way only member functions of the class have access to the private variables.

You defined "char alphabet[26]", but a "std::string" will work just as well.

Hope that helps,

Andy
Thank you so much, your response helped me a lot! Why did the '&' character send the values back to the original vector put in the function?
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

class functions
{
    private:
        char alphabet[26];
        string sentance;
        int counter=0;

    public:
        void collectAlphabet ()
        {
            ifstream file;
            file.open("alphabet.txt");

            for (int j=0; j<26;j++)
            {
                file >> alphabet[j];
            }
        }

        void input ()
        {
            cout << endl;
            cout << "Write a sentence: ";
            getline(cin,sentance);

        }

        void arrayFill (int x, vector <char> & tempArray)
        {
            for (int j=x;j<sentance.length();j+=4)
            {
                tempArray.push_back(1);
                tempArray[counter]=sentance[j];
                counter++;
            }

        }

        void arrayOutput (vector <char> tempArray)
        {
            for (int j=0;j<tempArray.size();j++)
            {
                cout << tempArray[j];
            }
        }

        void subCipher ()
        {
            cout << endl << "The new version is ";

            for (int i=0;i<sentance.length();i++)
            {
                if (sentance[i]==' ')
                    {
                        cout << " ";
                    }

                for (int j=0;j<26;j++)
                {
                    if (alphabet[j]==alphabet[23] && sentance[i]==alphabet[23])
                    {
                        cout << alphabet[0];
                    }

                    else if (alphabet[j]==alphabet[24] && sentance[i]==alphabet[24])
                    {
                        cout << alphabet[1];
                    }

                    else if (alphabet[j]==alphabet[25] && sentance[i]==alphabet[25])
                    {
                        cout << alphabet[2];
                    }

                    else if (alphabet[j]==sentance[i])
                    {
                        cout << alphabet[j+3];
                    }
                }
            }
            cout << endl << endl;
        }

        void multiArrayCipher()
        {
            vector <char> multiArrayOne;
            vector <char> multiArrayTwo;
            vector <char> multiArrayThree;
            vector <char> multiArrayFour;

            arrayFill(0,multiArrayOne);
            arrayFill(1,multiArrayTwo);
            arrayFill(2,multiArrayThree);
            arrayFill(3,multiArrayFour);

            cout << endl << "The new version is ";

            arrayOutput(multiArrayOne);
            arrayOutput(multiArrayTwo);
            arrayOutput(multiArrayThree);
            arrayOutput(multiArrayFour);

            cout << multiArrayOne[0] << endl << endl;
        }
};

int main()
{
    functions program;
    int input{};

    while (input!=5)
    {
        cout << "1. Substitution cipher" << endl;
        cout << "2. Multi-array cipher" << endl;
        cout << "3. Transposition cipher" << endl;
        cout << "4. Code breaker" << endl;
        cout << "5. Exit" << endl << endl;
        cout << "Enter the number of the option you want to proceed with: ";
        cin >> input;
        cin.ignore();

        switch (input)
        {
            case 1:
                program.collectAlphabet();
                program.input();
                program.subCipher();
                break;

            case 2:
                program.input();
                program.multiArrayCipher();
                break;


        }

    }


    return 0;
}


Does this code make better use of classes?
Hello Shezshade,

The "&" means that you are passing the variable by reference which means that you are using the variable that was passed to the function in the function call.

In this case the function"multiArrayCipher" creates the variable "vector <char> multiArrayOne" which is passed to the function "arrayOutput". Without using the "&" only a copy of the vector is passed to the function. In the case of this program this time chanced are made to the copy and do not get back to the original. Bassing by reference allows any changes to be reflected in the original. Then there is the question of the overhead it costs to make a copy of something. Sometimes it makes no difference, but in a large program it could save a lot of time to pass by reference.

The class is looking better with the variables in the private section. Although it is not common practice to initialize variables in the definition. This is reserved for the ctor. I would also change the character array to a "std::string", it works the same way.

I would write the class this way:
1
2
3
4
5
6
7
8
9
10
11
12
13
class functions
{
private:
	std::string alphabet;
	string sentance;
	int counter;

public:
	functions  // <--- Default ctor
	{
		alphabet = "ABC...Z";
		counter = 0;
	}


The other thing I would do is put the class in a header file and the functions in a ".cpp" file, but that is me.

The use of the function "collectAlphabet" is not needed. This can be done easier in the ctor when a class object is created.

Another way to write the for loop without the use of a file:
1
2
for (int j = 0, ch = 'A'; j < 26; j++, ch++)
	alphabet[j] = ch;

This is more for future reference.

In the "arrayFill" function line 40 should be written as tempArray.push_back(sentance[j]) and lines 41 and 42 would not be needed unless you would need the "counter" on line 42. In the first program I made this change and it worked fine.

Hope that helps,

Andy
Hello Shezshade,

I hope that the green check means that it is working for you, becaue I ran into problems with the "subCipher" function.

Andy
Topic archived. No new replies allowed.