Hello propvgvnda,
That is to bad that you do not know about "std::string"s yet. It makes it much easier.
The first problem:
char newKey[msgLen], encryptedMsg[msgLen], decryptedMsg[msgLen];
. This is known as a VLA, (variable length array), and is not allowed in C++ and not allowed by most newer compilers. A few very old compilers may allow this though.
What is in the []s needs to be a constant number like 12 or a variable defined as a constant like:
constexpr int MSGLEN{ 12 };
or you could just "const".
If you have a need for using
char newKey[msgLen]
you could use "magLen" to create a dynamic array using the "new" key word. But do not forget to include "delete" to free the memory before the program ends.
Before I can continue I need to know if you know how to create a dynamic array.
It just hit me. You say
char newKey[msgLen]
which would create an array of 12 elements, but you did not allow for the "\0" at the end. what you would need is:
char newKey[msgLen + 1]
to allow for the '\0' at the end.
Using "std::string"s your code would change to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
std::string msg{ "ATTACKATDAWN" };
std::string key{ "LEMON" };
size_t i, j;
std::string newKey, encryptedMsg, decryptedMsg;
//generating new key
for (i = 0, j = 0; i < msg.size(); ++i, ++j)
{
if (j == key.size())
j = 0;
newKey += key[j];
}
|
Here you do not have to worry about sizes or arrays, but you can still access the string the same as you would an array.
Something to think about and if you want some information:
http://www.cplusplus.com/reference/string/string/
https://en.cppreference.com/w/cpp/string
https://www.learncpp.com/cpp-tutorial/4-4b-an-introduction-to-stdstring/
or the home page
https://www.learncpp.com Very useful.
I will try working with what you started with.
Andy