Menu

Using C Luhn Algorithm Fix Cards Swap Errors Facing Swap Error Pair Adjacent Digits Swappe Q43802280

Using c++ and the Luhn Algorithm, I have to fix cards with swaperrors.  When facing a swap error, a pair of adjacentdigits are swapped until the right card combination is found. I amto find the leftmost pair which, if swapped, makesvalid card number. The checkLuhn function get the sum of all of thevalues in a string. I use that sum to check if the card are validby using checkLuhn % 10. In addition, I take from stdin the numberof cards the user needs to check. My issue is that my program workwhen I hard code a value inside a single string value, and applythe checkLuhn on it. However, when I try to use the function insidea loop to make operation equals to use input, I get 1 of the inputcards wrong( here 25535146….).In addition, I also getsegmentation fault for large data inputs. Below is the working hardcoded program along the version which requires user input. I haveattached pictures of the output I get. Your feedback will beappreciated.

sample input data:

2

1217400151414995

2553514623364925

sample answer:

1217040151414995

2553514623369425

_____________________________________________________________________________________________________________

//Hard coded version

#include <iostream>

#include <string>

#include <vector>

#include <array>

#include <limits>

using namespace std;

void replace(char* a, char* b)

{

*b = *a;

}

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;

}

int main(){

string x = “2553514623364925”;

int n = 0;

int nn = 1;

if(checkLuhn(x) != 0){

while(checkLuhn(x) % 10 != 0){

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

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

}

else

{

swap(x[n – 2],x[nn – 2]);

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

}

n = n + 2;

nn = nn + 2;

}

}

cout << endl;

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

{

cout << x[i];

}

return 0;

}

2553514623369425:

———————————————————————————-

//same but with added user input

#include <iostream>

#include <string>

#include <vector>

#include <array>

#include <limits>

using namespace std;

void replace(char* a, char* b)

{

*b = *a;

}

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;

}

int main()

{

int count;

string x;

int s = 0;

int n = 0;

int nn = 1;

cin >> count;

cin.clear();

cin.ignore(numeric_limits<streamsize>::max(), ‘n’);

vector <string> input;

while(getline(cin,x) && s < count)

{

input.push_back(x);

s++;

}

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

{

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

{

while(checkLuhn(input[j]) % 10 != 0)

{

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

{

swap(input[j][n],input[j][nn]);

}

else

{

swap(input[j][n – 2],input[j][nn – 2]);

swap(input[j][n],input[j][nn]);

}

n = n + 2;

nn = nn + 2;

}

}

}

cout << endl;

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

{

cout << input[x];

cout << endl;

}

return 0;

}

1217400151414995 2553514623364925 1217040151414995 2553154632364925

2553514623369425: 1217400151414995 2553514623364925 1217040151414995 2553154632364925 Show transcribed image text 2553514623369425:
1217400151414995 2553514623364925 1217040151414995 2553154632364925

Expert Answer


Answer to Using c++ and the Luhn Algorithm, I have to fix cards with swap errors. When facing a swap error, a pair of adjacent dig…

OR