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:
{#&@
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.
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.
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.
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.
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!