Need help on my code

Hi,

I'd like to ask help on this project(school project) of mine.

The problem that was given to me is to decipher this message "YH RWJAFQC HHVBQIPEACHS VR ATZ NPU YFUXQKLWCTRQG" to "WE PRESENT CERTIFICATES TO YOU FOR PARTICIPATING".

Basically, the operation was to count the number of characters, not including spaces or punctuation in the message. Then divide the number of characters by 100, look at the remainder and, if it is prime, use it for the next step or else take the next prime greater than the number. Lastly, find the reciprocal of the prime and, disregarding any leading zeros, use the digits in the reciprocal to determine the letter shifts used for enciphering the message.

I am advised to use keys for this but I don't know how to create one. So I did it the hard way. I've already deciphered the first three words but I don't know why the rest of words won't follow.

Below is my code:


#include <iostream>
using namespace std;
int main()
{
char msg[50];
bool b=false;
int numchar=0,ctr,lol[50];
long double r1,r2,r3;
cout<<"Enter message: ";
cin.get(msg,50);

for (int a=0;a<50;a++)
{
msg[a]=toupper(msg[a]);
if (msg[a]>=65&&msg[a]<=90)
numchar++;
}

do
{
if (numchar==1)
b=true;
else
{
ctr=0;
for (int a=1;a<=numchar;a++)
{
if (numchar%a==0)
ctr++;
}
if (ctr==2)
b=true;
else
numchar++;
}
}

while (b!=true);
r3=(long) (double(numchar));
r1=r3;
r2=1/r1;
for(int a=0;a<numchar;a++)
{
if (r2==1)
lol[a]=1;
else
{
r2*=10;
lol[a]=(int(r2));
r2=(r2-lol[a]);
if (lol[0]==0)
a=-1;
}
}

ctr=0;

for (int a=0;a<=50;a++)
{
if (msg[a]>=65&&msg[a]<=90)
{
int exp=0;
msg[a]-=lol[ctr];
ctr++;
if (msg[a]<65)
{
while (msg[a]<64)
{
exp++;
msg[a]++;
}
msg[a]='Z';
msg[a]-=exp;
}
cout<<msg[a];
}
else if ((msg[a]>=33&&msg[a]<=64)||(msg[a]>=91&&msg[a]<=126))
cout<<msg[a];
else
cout<<" ";
}
cout<<endl;
}

Output of this code: WE PRESENT CERTIFICATEP OL YOY FIU XETSLBGOZLLE :(

I'd much appreciate if you could help me on this. Thanks!
Please indent your code correctly and use the [ code] [. /code] tags located on the right side of your screen under "Format". Makes code a lot easier to read, thanks!
Here it is.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
using namespace std;
int main()
{
    char msg[50];
    bool b=false;
    int numchar=0,ctr,lol[50];
    long double r1,r2,r3;
    cout<<"Enter message: ";
    cin.get(msg,50);

    for (int a=0;a<50;a++)
    {
        msg[a]=toupper(msg[a]);
        if (msg[a]>=65&&msg[a]<=90)
        numchar++;
    }

    do
    {
        if (numchar==1)
        b=true;
        else
        {
            ctr=0;
            for (int a=1;a<=numchar;a++)
            {
                if (numchar%a==0)
                ctr++;
            }
        if (ctr==2)
        b=true;
        else
        numchar++;
        }
    }

    while (b!=true);
    r3=(long) (double(numchar));
    r1=r3;
    r2=1/r1;
    for(int a=0;a<numchar;a++)
        {
            if (r2==1)
            lol[a]=1;
            else
            {
                r2*=10;
                lol[a]=(int(r2));
                r2=(r2-lol[a]);
                if (lol[0]==0)
                a=-1;
            }
        }

    ctr=0;

    for (int a=0;a<=50;a++)
        {
            if (msg[a]>=65&&msg[a]<=90)
            {
                int exp=0;
                msg[a]-=lol[ctr];
                ctr++;
                if (msg[a]<65)
                {
                    while (msg[a]<64)
                        {
                            exp++;
                            msg[a]++;
                        }
                    msg[a]='Z';
                    msg[a]-=exp;
                }
                cout<<msg[a];
            }
            else if ((msg[a]>=32&&msg[a]<=64)||(msg[a]>=91&&msg[a]<=126))
            cout<<msg[a];
            else
            cout<<" ";
        }
    cout<<endl;
}
Looks like no one would want to help. Thanks anyway!
They(We) will, as soon as we figure out a good way to present a very illustrative explanation.
Topic archived. No new replies allowed.