Code:
# include & lt; iostream & gt;
#include & lt; string & gt;
using namespace std;
class Vigenere
{
public:
string key;
Vigenere (string key)
{
for (int i = 0; i & lt; key.size (); ++ i)
{
if (key [i] & gt; = 'A' & amp; & amp; key [i] & lt; = 'Z')
this- & gt; key + = key [i];
else if (key [i] & gt; = 'a' & amp; & amp; key [i] & lt; = 'z')
this- & gt; key + = key [i] + 'A' - 'a';
}
}
string encrypt (string text)
{
string out;
for (int i = 0, j = 0; i & lt; text.length (); ++ i)
{
char c = text [i];
if (c & gt; = 'a' & amp; & amp; c & lt; = 'z')
c + = 'A' - 'a';
else if (c & lt; 'A' || c & gt; 'Z')
continue;
out + = (c + key [j] - 2 * 'A')% 26 + 'A';
j = (j + 1)% key.length ();
}
return out;
}
string decrypt (string text)
{
string out;
for (int i = 0, j = 0; i & lt; text.length (); ++ i)
{
char c = text [i];
if (c & gt; = 'a' & amp; & amp; c & lt; = 'z')
c + = 'A' - 'a';
else if (c & lt; 'A' || c & gt; 'Z')
continue;
out + = (c - key [j] + 26)% 26 + 'A';
j = (j + 1)% key.length ();
}
return out;
}
};
int main ()
{
Vigenere cipher ("VIGENERE");
string original = "test";
string encrypted = cipher.encrypt (original);
string decrypted = cipher.decrypt (encrypted);
cout & lt; & lt; original & lt; & lt; endl;
cout & lt; & lt; "Encrypted:" & lt; & lt; encrypted & lt; & lt; endl;
cout & lt; & lt; "Decrypted:" & lt; & lt; decrypted & lt; & lt; endl;
}
When I compile this code, I get errors:
(12): warning C4018: & lt ;: signed and unsigned type mismatch
(25): warning C4018: & lt ;: signed and unsigned type mismatch
(45): warning C4018: & lt ;: signed and unsigned type mismatch
How can you decide?
Answer 1, authority 100%
Use not int
, but size_t
– the type that returns .size ()
…
No, of course, there is still an option
# pragma warning (disable: 4018)
and you won’t get any relevant messages at all 🙂