-1

I am trying to write a program that takes in 2 string inputs and assigns a numeric value to each letter in the string input. The program then multiples the numeric values assigned to the letters in the string inputs and sees if the product of the values (mod 47) of both inputs are equal. If they are equal, then the program must output "yes" if not, then it must output "NO".

I am using a brute force method, so please feel free to suggest more efficient methods and also how to implement them. I have reached a point where my IDE only gives several warnings, but no errors. However when I run the program, I get an illegal instruction, which is essentially because of a particular function, which I will show later.

I WOULD PREFER THAT THE SOLUTIONS TO MY QUESTION DON'T INCLUDE ANY EXTRA CLASS LIBRARIES AND FILES. THANK YOU ;)

I've tried using a brute force method e.g in this long function:

This takes in values from a function of return type int which assigns the numeric value to the 6 letter input, and returns them to a variable of return type bool, which finds and compares the final product of the two strings before returning a value of type bool. I've used the brute force method of comparing each individual letter of the string to a letter and assigning a value. I am quite sure that the first function isn't right, although I don't know how to fix it.

int parse(const char * x)
{

    if (x == "A")
    {
        return 1;
    }
    if (x == "B")
    {
        return 2;
    }
    if (x == "C")
    {
        return 3;
    }
    if (x == "D")
    {
        return 4;
    }
    if (x == "E")
    {
        return 5;
    }
    if (x == "F")
    {
        return 6;
    }
    if (x == "G")
    {
        return 7;
    }
    if (x == "H")
    {
        return 8;
    }
    if (x == "I")
    {
        return 9;
    }
    if (x == "J")
    {
        return 10;
    }
    if (x == "K")
    {
        return 11;
    }
    if (x == "L")
    {
        return 12;
    }
    if (x == "M")
    {
        return 13;
    }
    if (x == "N")
    {
        return 14;
    }
    if (x == "O")
    {
        return 15;
    }
    if (x == "P")
    {
        return 16;
    }
    if (x == "Q")
    {
        return 17;
    }
    if (x == "R")
    {
        return 18;
    }
    if (x == "S")
    {
        return 19;
    }
    if (x == "T")
    {
        return 20;
    }
    if (x == "U")
    {
        return 21;
    }
    if (x == "V")
    {
        return 22;
    }
    if (x == "W")
    {
        return 23;
    }
    if (x == "X")
    {
        return 24;
    }
    if (x == "Y")
    {
        return 25;
    }
    if (x == "Z")
    {
        return 26;
    }
}

bool returnfunc(std::string & GROUP, std::string & k)
{

    const char A = GROUP[0];

    const char B = GROUP[1];

    const char C = GROUP[2];

    const char D = GROUP[3];

    const char E = GROUP[4];

    const char F = GROUP[5];

    int xet = parse(&A);

    int r = parse(&B);

    int m = parse(&C);

    int z = parse(&D);

    int h = parse(&E);

    int j = parse(&F);
    double mu = (xet * r * m * z * h * j) % 47;

    const char G = k[0];
    const char H = k[1];
    const char I = k[2];
    const char J = k[3];
    const char K = k[4];
    const char L = k[5];
    int w = parse(&G);
    int x = parse(&H);
    int y = parse(&I);
    int a = parse(&J);
    int b = parse(&K);
    int c = parse(&L);

    double fin = (w * x * y * a * b * c) % 47;
    {
        if (mu == fin)

        {
            return true;
        }

        else
        {
            return false;
        }
    }
}

/*I expected the program to run but it didn't and I don't understand 
 why.*/
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 2
    Lot of code you got there. You'll make the question more useful to those who come later by playing a few rounds of divide and conquer and turning it into a [mcve]. – user4581301 Aug 07 '19 at 04:47
  • Here's a cheap trick that usually ([but not always](https://en.wikipedia.org/wiki/EBCDIC)) works: `letter-'A'`. If the character encoding in use has a sensible ordering of the letters if you subtract `'A'` from a letter, you get the offset of that letter in the alphabet. Add 1 and you'll get the numbers you seem to want without the huge function of `if`s. – user4581301 Aug 07 '19 at 04:58
  • Pointers are easy. *x == *"A" – ChuckCottrill Aug 07 '19 at 05:01
  • 2
    `I have reached a point where my IDE only gives several warnings, but no errors.` Warnings can be useful for debugging this type of thing. What warnings do you get? –  Aug 07 '19 at 05:02
  • @Chuck, works, but much more effort than fixing `parse` to take a single character and compare against a character literals instead of a string literals. – user4581301 Aug 07 '19 at 05:06
  • Easier. *x == 'A' – ChuckCottrill Aug 07 '19 at 05:31

3 Answers3

4

This is wrong

if (x=="A")

because it compares two pointers for pointer equality. It does not compare what those pointers are pointing at (which is what you want to do).

Here's a simpler version of your code that keeps the same structure but does not involve any pointers (for some reason newbies love pointers, given how difficult they are I've never understood why).

int parse(char x)
{
    if (x == 'A')
        return 1;
    if (x == 'B')
        return 2;
    // etc etc
}

Then

int xet = parse(A);
int r = parse(B);
// etc etc

PS, the more consise way to write your parse function would be to use a lookup table and a loop

int parse(char x)
{
    const char* table = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (int i = 0; i < 26; ++i)
        if (x == table[i])
            return i + 1;
    return 0; // error return
}
john
  • 85,011
  • 4
  • 57
  • 81
1
int parse(const char * x) {
    if (x == "A")

x is a const char* and "A" is a const char* but they will not be equal even if they point at C strings with the same characters since the comparison compares the addresses of the C strings, not the contents.

Also, if your parse function does not find a match it will leave the function without returning a value which is Undefined Behaviour.

Try this instead:

int parse(char x) {
    if(x >= 'A' && x <= 'Z') return x - 'A' + 1;
    else return 0;
}

And call parse with the char itself as an argument (instead of a pointer to it):

const char A = GROUP[0];
int xet = parse(A);
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

It's hard to answer this mostly because of this part of your question:

I have reached a point where my IDE only gives several warnings, but no errors.

There may be a warning that we don't know about that might be part of your problem. However, I'm going to take a stab anyway at a couple of things that are wrong.

So, first is this line right here:

int parse ( const  char * x)

Now in and of itself, this isn't bad, except it's coupled with something like this:

if(x=="A")
{
  return 1;
}

That is all sort of bad. x is a pointer. That means that the value store in x is an address. That means you are comparing the c string "A" to an address. That's not usually a good thing to do.

What you want is probably this instead:

if(*x == "A")

That's much better.

Well, not quite, actually, because you probably want single quotes here instead of double quotes:

if(*x == 'A')

By the way, you could probably simplify your parse function as follows, and it'd do the same thing:

int parse ( const  char * x) {
    return (*x - 'A') + 1; // parenthesis for clarity. Not really needed here.
}