Question: Using c++ and the Luhn Algorithm, I have to fix cards with swap errors. When facing a swap error, a pair of adjacent digits are

Using c++ and the Luhn Algorithm, I have to fix cards with swap errors. When facing a swap error, a pair of adjacent digits are swapped until the right card combination is found. I am to find the leftmost pair which, if swapped, makes valid card number. The checkLuhn function get the sum of all of the values in a string. I use that sum to check if the card are valid by using checkLuhn % 10. In addition, I take from stdin the number of cards the user needs to check. My issue is that my program work when I hard code a value inside a single string value, and apply the checkLuhn on it. However, when I try to use the function inside a loop to make operation equals to use input, I get 1 of the input cards wrong( here 25535146....).In addition, I also get segmentation fault for large data inputs. Below is the working hard coded program along the version which requires user input. I have attached pictures of the output I get. Your feedback will be appreciated.

sample input data:

2

1217400151414995

2553514623364925

sample answer:

1217040151414995

2553514623369425

_____________________________________________________________________________________________________________

//Hard coded version

#include

#include

#include

#include

#include

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

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

for(int i = 0; i

{

cout

}

return 0;

}

Using c++ and the Luhn Algorithm, I have to fix cards with

----------------------------------------------------------------------------------

//same but with added user input

#include

#include

#include

#include

#include

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

{

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::max(), ' ');

vector input;

while(getline(cin,x) && s

{

input.push_back(x);

s++;

}

for(int j = 0; 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

for(int x = 0; x

{

cout

cout

}

return 0;

}

swap errors. When facing a swap error, a pair of adjacent digits

2553514623369425: 1217400151414995 2553514623364925 1217040151414995 2553154632364925

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!