-1

So I'm back with my school project. The teacher gave us more code to work with. A hexadecimal calculator. At first glance it looks finished but... the math is wrong. Can someone look over this code and tell me what's wrong or what can I do to make it right? I need to keep most of this code as is. So I can't change the principle. I must stick with this. When I run this code I do get a result, but it's not the result I need. I need to do something like "1cec+bec = 28d8". Again, I don't get errors, but I don't get the right result.

#include <iostream>
#include <cstring>

using namespace std;

int test(char[], int);
void decal(char[], int n);
int add2(char a, char b, int r);
void add(char a[], char b[], char c[]);

int main()
{
    char a[10], b[10], c[10];
    int valid;

    do {
        cout << "Insert first number (maximum 5 char hexa):" << endl;
        cin >> a;
        valid = test(a, strlen(a));
        if(!valid)
            cout << "Error." << endl;
    } while (!valid);
    cout << "First number: " << a << endl;
    decal(a, strlen(a));
    cout << "First number after completing: " << a << endl;

    do {
        cout << "Insert 2nd number (maximum 5 char hexa):" << endl;
        cin >> b;
        valid = test(b, strlen(b));
        if(!valid)
            cout << "Error." << endl;
    } while (!valid);

    decal(b, strlen(b));
    cout << "2nd number: " << b << endl;

    add(a, b, c);  
    cout << "Rezultat: " << c << endl;
    return 0;
}

void add(char a[], char b[], char c[])
{
    int i, r=0, sab;
    c[6] = '\0';
    for(i = 4; i>= 0; i--)
    {
        sab = add2(a[i], b[i], r);  
        if(sab < 10)
        {
            r = 0;
            c[i+1] = sab + 48; // '0' ... '9'
        }

        else
            if(sab < 16)
            {
                r = 0;
                c[i] = 97 + (sab - 10); // 'a' ... 'f'
            }

            else
            {
                r = 1;
                sab = sab - 16;
                if(sab < 10)
                {
                    r = 0;
                    c[i+1] = sab + 48; // '0' ... '9'
                }
                else
                {
                    r = 0;
                    c[i] = 97 + (sab - 10); // 'a' ... 'f'

                }
            }
    }
    c[0] = 48 + r;
}

int add2(char a, char b, int r)
{
    int ai, bi, ci, ai1, bi1;
    ai = tolower(a);
    if(ai <= 57)
        ai1 = ai - 48;
    else
        ai1 = ai - 97 + 10;
    bi = tolower(b);
    if(bi <= 57)
        bi1 = bi - 48;
    else
        bi1 = bi - 97 + 10;
    ci = ai1 + bi1;
    return ci;
}

int test(char x[], int n)
{
    if(n > 5)
        return 0;  

    for(int i = 0; i < n; i++)
    {
        if(x[i] <48 || (x[i] > 57 && x[i] < 65) || (x[i] > 70 && x[i] < 97) || x[i] > 102)
            return 0;  
    }
    return 1;
}

void decal(char x[], int n)
{

    int i, nz;
    x[5] = '\0';
    nz = 5 - strlen(x); 
    if(nz > 0) {
        for(i = 0; i < n; i++)
            x[5 - i-1] = x[n-i-1];
    }

    for(i = 0; i < nz; i++)
            x[i] = '0';
}
  • 6
    This is a great opportunity for you to learn how to use your debugger. See [how to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Hatted Rooster Jun 15 '19 at 11:30
  • @SombreroChicken it's not an issue about debugging. I get no errors and the debugger also doesn't help. I probably wrote something that isn't right mathematical. That's why I kinda need it fixed – BuffalloKing Jun 15 '19 at 11:38
  • 3
    ***I get no errors and the debugger*** You are not supposed to get errors. The debugger is a tool that lets you execute your code 1 line at a time looking at your variables after each line executes. If you know how your code is supposed to work you should be able to see exactly where is deviates from your expectation. – drescherjm Jun 15 '19 at 11:56
  • 1
    My one comment on your code is you should avoid all those magic numbers that may be obvious when you create your program but not so obvious when you are reading in months from now. https://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad – drescherjm Jun 15 '19 at 11:58
  • @drescherjm I know. If it were up to me I would write this completly different and be done with a calculator in 10 lines or so. But the teacher makes us write it like that for "didactic" purpose, even if nobody in real world would write it like that. I just want a fix on that code cause I can't figure it out myself – BuffalloKing Jun 15 '19 at 12:02
  • I would have to use my debugger to figure out what is wrong with this code. I doubt I could do the debugging in my head. – drescherjm Jun 15 '19 at 12:05
  • @drescherjm yup. Sorry. about that. I fixed it above too. Was in a rush. Still have no clear answer yet though – BuffalloKing Jun 15 '19 at 16:32

1 Answers1

1

add2 takes r (which appears to be meant as a carry bit) as a parameter, but doesn't actually use it.

In any case, the way you compute r doesn't make sense. There's exactly one place where it's set to 1, but then it's unconditionally reset back to 0 a couple lines later.

The bottom line is, you are dropping all carries.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
  • Then, how should the code be written to make sense? – BuffalloKing Jun 16 '19 at 08:57
  • 1
    Nice try, but I'm not going to do your homework for you. Recall how you performed long addition in elementary school, with paper and pencil. Same algorithm here. – Igor Tandetnik Jun 16 '19 at 13:44
  • Fine, I'll give it a rest. On a side note... This is more like a suplement course. I have no c++ knowledge at all, so that's why I wanted someone to fix that code. I just want a passing grade. Again, thanks for trying to teach me but... c++ and me don't go well. – BuffalloKing Jun 16 '19 at 15:55
  • @BuffalloKing My advice is to figure out the math required with a pen and paper. That will make it simpler to implement in code. – drescherjm Jun 16 '19 at 16:21