Decrypting a Message

May 4, 2020 at 5:19pm
Hello. I am currently learning about vectors and arrays. I am working on an assignment where I need to decrypt secret messages using cipher. I am struggling in step 1 on how to decrypt all instances of '{' to 'n'. My first guess was that I need to use a for loop to iterate through the vector but I am not positive. I am sure there is an easy fix but I am just stumped. I am not looking for the answer, just some tips. I included the code I have so far and the steps to the assignment. Thank you.

1) Decrypt by hand the following secret message:{#&@
Then, modify the program to decrypt all instances of ‘{‘ in the secret message to ‘n’.

2) Modify the program to decrypt all instances of 3 more decryptions:
‘&’ ­> ‘c’
‘@’ ­> ‘e’
‘#’ ­> ‘i’
Your program should decrypt the following secret message:
{#&@

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
  #include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    vector<char> normalV(26);
    vector<char> cipherV(26);
    string toDec = "";
    string beenDec = "";
    
    normalV.at(0) = 'n'; cipherV.at(0) = '{';
    normalV.at(1) = 'i'; cipherV.at(1) = '#';
    normalV.at(2) = 'c'; cipherV.at(2) = '&';
    normalV.at(3) = 'e'; cipherV.at(3) = '@';
    
    // Get secret message
    do {
        cout << "Enter a secret message: ";
        getline(cin, toDec);
    } while (toDec.length() == 0);

    beenDec = toDec;

    // Decrypt secret message
    if (toDec.at(0) == cipherV.at(0)) {
        beenDec.at(0) = normalV.at(0);
    }
    if (toDec.at(1) == cipherV.at(1)) {
        beenDec.at(1) = normalV.at(1);
    }
    if (toDec.at(2) == cipherV.at(2)) {
        beenDec.at(2) = normalV.at(2);
    }   
    if (toDec.at(3) == cipherV.at(3)) {
        beenDec.at(3) = normalV.at(3);
    }
    
    cout << "Decrypted message: " << beenDec << endl;
    
    return 0;
}
Last edited on May 4, 2020 at 5:21pm
May 4, 2020 at 6:36pm
My first guess was that I need to use a for loop to iterate through the vector but I am not positive.

you can, and usually should, use [0] instead of .at(0). at is safer, but it does check things which slows it down, so only use if unsure that your index is correct.

you can use a for loop, but c++ has tools like .find() that may serve you better. (these still iterate, they just do simple stuff for you).
This may also be better done in string than vector. But it will work in either. Nothing wrong with string nice = "nice"; [0] is still 'n' and so on.

also, just tie them together and make it simple: if you had all 26 or worse cased 52 letters and more symbols etc it would be a nightmare with if statements.
consider:
char[256] xlat = {0};
char['{'] = 'n'; //set these up once
cout << xlat[inputstring[index]]; //if this is '{' it will write 'n' ..

you can hard code the initialization of xlat, making the whole program 5 lines long or so.
Last edited on May 4, 2020 at 6:37pm
May 4, 2020 at 7:40pm
Hello fruhtie,

jonnin has good ideas that would be better for the program as a whole, but that does not cover learning "vector"s.

Here is a start to what you have that should work better for you. I would not say that it is perfect, but it does show ways of shorting your 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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
	//vector<char> normalV(26);
	//vector<char> cipherV(26);
	vector<char> normalV{ 'n', 'i', 'c', 'e' };
	vector<char> cipherV{ '{', '#', '&', '@' };
	string toDec{ "{@&#" };  // <--- Used for testing. Reverse comments or remove when finished.
	//std::string toDec{ "{#&@" };  // <--- Used for testing. Remove { to } when done.
	//std::string toDec{ "#&@" };
	//std::string toDec{ "&#{" };
	string beenDec;

	//normalV.at(0) = 'n'; cipherV.at(0) = '{';
	//normalV.at(1) = 'i'; cipherV.at(1) = '#';
	//normalV.at(2) = 'c'; cipherV.at(2) = '&';
	//normalV.at(3) = 'e'; cipherV.at(3) = '@';

	// Get secret message
	//do  // <--- Commented for testing. Remove comments when done.
	//{
	//	cout << "Enter a secret message: ";
	//	getline(cin, toDec);
	//} while (toDec.length() == 0);

	//beenDec = toDec + ' ';  // <--- Should put a space here if you are going to use this. 

There is no reason to create a vector of 26 elements when you are only using 4 at the moment. Also if you need to each vector can be added to without all the unused elements wasting space.

To me line 29 is not necessary unless you want the encrypted and decrypted parts on one line then the space makes it easier to read.

After this I used a nested for loops. the outer to traverse the string and the inner to traverse the vector "cipherV". This way it does not matter what order the encrypted characters are in you still decrypt to the correct letters.

Lines 13 and 16 are used for testing. Reverse the // for different output/

I put the comment on lines 22 - 27already know that it works. This is so you do not have to type something each time the program runs just to test it.

I will give you a chance to come up with something for the for loops.

Andy
May 4, 2020 at 11:28pm
Thank you, this helps a lot. I have to submit one step at a time so I should be focusing on just '{' first. I created a nested for loop and this is what I have now. I just basically need to finish the if statement.

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
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    int i = 0;
    int j = 0;
    vector<char> normalV(26);
    vector<char> cipherV(26);
    string toDec = "";
    string beenDec = "";
    
    normalV.at(0) = 'n'; cipherV.at(0) = '{';
    normalV.at(1) = 'i'; cipherV.at(1) = '#';
    normalV.at(2) = 'c'; cipherV.at(2) = '&';
    normalV.at(3) = 'e'; cipherV.at(3) = '@';
    
    // Get secret message
    do {
        cout << "Enter a secret message: ";
        getline(cin, toDec);
    } while (toDec.length() == 0);

    beenDec = toDec;

    // Decrypt secret message
    if (toDec.at(0) == cipherV.at(0)) {
        beenDec.at(0) = normalV.at(0);
    }
    if (toDec.at(1) == cipherV.at(1)) {
        beenDec.at(1) = normalV.at(1);
    }
    if (toDec.at(2) == cipherV.at(2)) {
        beenDec.at(2) = normalV.at(2);
    }   
    if (toDec.at(3) == cipherV.at(3)) {
        beenDec.at(3) = normalV.at(3);
    }
    
    for (i = 0; i > normalV.size(); ++i) 
    {
        for (j = 0; j > cipherV.size(); ++j)
        {
            if ()
            {
            beenDec.at(i) = normalV.at(j);
                }
            }
        }
        
            
    cout << "Decrypted message: " << beenDec << endl;
    
    return 0;
}
Last edited on May 4, 2020 at 11:29pm
May 5, 2020 at 1:36am
Hello fruhtie,

On lines 9 and 10 you define and set the vector with 26 elements. Since you are only using 4 what are the other unused 22 elements for?

I showed you how to define and initialize the vector. This was to eliminate lines 14 - 17.

The nested for loops was to eliminate the preceding if statements.

Looking at the for loops. You should define "i" and "j" in the for loop not outside. Now if "i" = 0 and "normalV.size()" returns a value of 26. How can 0 be > 26? The same is true for the inner for loop.

Also the for loop should be for (size_t i = 0; i < whatToCheck.size(); i++). "size_t" is an alias for an "unsigned int" and the is what is returned by the ".size()" and ".length()" functions and some others that are member function of STL classes.

Now thinking about what you have to check. first you enter something into the string "toDec". In a way you can think of the string as being an array of single characters. You will need to go through each element of the string and compare it to the "cipherV" elements until you find a match. When you find a match in "cipherV" you can use "j" to access the same element of "normalV" and then add that element of "normalV" to the string "beenDec".

When you get the for loops set up correctly the if statement will be easier to figure out.

Andy
May 5, 2020 at 2:11am
Thank you, I get it now. Also I was given starter code to jumpstart this assignment and the 22 unused elements are supposed to come in later for step 3 which I did not post. Thank you for all your help!
Topic archived. No new replies allowed.