SwappingError with Luhn Algorithm

Back to Problem Solutions forum

SkullxLuv     2020-01-07 23:32:58

Hi! I am struggling with my swap error function with Luhn Algorithm. I have one function (checkLuhn()) in c++ that checks for the sum of all values inside a given string. In addition, I have a function (fixcard()) that take a string, traverse it from left to right, finding the Luhn sum of the string and determine if changes are needed. However, some of my outputs are right, others are not. Can somebody lend my project some feedbacks of what I can do to fix it.

P.S: I did not include the function that checks if string contains "?" because that one works.

int checkLuhn(string myString)

{

int x = myString.size();

int sum = 0;

int i = 0;

char c = ' ';

for(int v = x - 1; v >= 0; --v)

{

    if(v % 2 == 0)

    {

        i = (int)myString[v] - 48;

        i = i * 2;

        if(i > 9)

        {

            i = i - 9;

        }

        c = '0' + i;

        replace(&c , &myString[v]);

    }

}

for(int ii = 0; ii < x; ii++)

{

    sum = sum + (int)myString[ii] - 48;

}

return sum;

}

void fixCard(string card)

{

int n = 0;

int nn = 1;



if(checkLuhn(card) != 0)

{

    while(checkLuhn(card) % 10 != 0&&n<card.length()&&nn<card.length())

    {

        if(n == 0 || nn == 1)

        {

            swap(card[n],card[nn]);

        }

        else if(n >= 0 || nn >= 1)

        {

            swap(card[n - 2],card[nn - 2]);

            swap(card[n],card[nn]);

        }

        n = n + 2;

        nn = nn + 2;

    }

}

for(int i = 0; i < card.size(); i++)

{

    cout << card[i];

}

cout << endl;

} int main()

{

int count;

string x;

int s = 0;

cin >> count;

cin.clear();

vector<string> input;
while(getline(cin,x) && s < count)

{

    input.push_back(x);

    s++;

}
cout << endl;

string sign = "?";

char check = '?';

for(int j = 0; j < count; j++)

{

    size_t found = input[j].find(check);

    /*if (found != string::npos)

    {

        int swapError = findSwapError(input[j], found);

        char swapNumber = swapError + 48;

        input[j][found] = swapNumber;

        cout << input[j] << endl;

    }*/

    else if(checkLuhn(input[j]) != 0)

    {

        fixCard(input[j]);

    }

}



cout << endl;
return 0;

}

Rodion (admin)     2020-01-10 07:53:11
User avatar

Hi Friend!

I think I can try debugging your code, however it would be much more profitable for your "proper learning path" if you try returning to this task bit later. This problem is of a bit advanced level (I'd say it should go easier for one who solved 40-60 simpler tasks) so don't be frustrated too much.

Your code is bit more complicated than necessary (sometimes because you are still building up experience) and it is easy to get entangled... So perhaps you may want to rewrite it simpler :)

for example:

if(n == 0 || nn == 1)

I believe in this function nn is just n+1?

The approach seems to be correct, so it is probably just the flaw of implementation... Print out each of numbers being tested and see - perhaps you are just missing something?

    n = n + 2;
    nn = nn + 2;

perhaps, because of this.

Please login and solve 5 problems to be able to post at forum