Help with encryption? Might have something to do with for loop?
Apr 22, 2015 at 1:10am UTC
So, if you type in hello, for example, it should output
114
140
58
58
144
If you type in one letter at a time, it works perfectly.
However, if you type it in all together, it's giving me the wrong numbers.
Thank you so much!!
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
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
#include <math.h>
using namespace std;
int main()
{
cout << "Enter message to encrypt: " << endl;
string msg;
getline (cin,msg);
int length = msg.length();
int letter;
int c=1;
for (int i=0; i<length; i++)
{
letter=msg[i];
int n;
for (n=0; n<43; n++)
{
c=(letter*c)%341;
}
cout << c <<endl;
}
return 0;
}
Apr 22, 2015 at 1:29am UTC
I suspect it has to do with c's value after
1 2 3 4
for (n=0; n<43; n++)
{
c=(letter*c)%341;
}
I simply added
c = 1;
after cout << c <<endl;
and it's working just fine now:)
If you're still having some trouble, let me know.
Apr 22, 2015 at 12:36pm UTC
windows.h should be the first header file.
You don't reset c after each use.
Your code should look like this:
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
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
using namespace std;
int main()
{
cout << "Enter message to encrypt: " << endl;
string msg;
getline(cin, msg);
size_t length = msg.length();
for (size_t i = 0; i < length; ++i)
{
int letter = msg[i];
int c = 1;
for (int n = 0; n < 43; ++n)
{
c = (letter*c) % 341;
}
cout << c <<endl;
}
return 0;
}
Topic archived. No new replies allowed.