Write C Program Fixes List Bank Card Numbers Miss One Digits Restore Digit Others Pair Adj Q43794613
Write a c++ program that fixes a list of bank card numbers. Someof them just miss one of digits and you should restore this digit.Others have a pair of adjacent digits swapped – you are to find theleftmost pair which, if swapped, makes valid cardnumber.
All the card numbers would be 16 digits long.
Input data contain a list of workers who havetheir card numbers incorrect.
Next lines contain a single card number each. If the numbercontains “?” (question mark) instead of some digit – this digitshould be restored. If all digits are present, then “swap-error”should be fixed.
Answer should contain a list of “fixed” cardnumbers.
input data:
4
?942682966937054 1217400151414995 2146133934?671142553514623364925
answer:
3942682966937054 1217040151414995 21461339346671142553514623369425
______________________________________________________________________________________
I need some help with my program. It is able to find a value toreplace the “?” with, but it does not find the right value for allof the test cases above. I also wrote a function(not included inbelow code), which use if statement to branch when the string doesnot contain “?” and deal with swap errors. For that, my approachwas to swap adjacent values, but I was able to get the right valueoutput to match the output sample above. I believe a map would behelpful to deal with this program, but I am not familiar with mapas data structures. I would appreciate your input. Since this is myfirst time trying to work with map, I would appreciate commentsinside the code. Also, I use Luhn Algorithm to check if a card isvalid or not.
—————————————————————————————————————————————————
#include <iostream>
#include <string>
#include <vector>
#include <array>
using namespace std;
void replace(int* a, int* b)
{
*b = *a;
}
void replace2(char* x, char* y)
{
*y = *x;
}
void eraseDemo(string str, int index)
{
str.erase(str.begin() + index);
}
int checkLuhn(string myString)
{
int x = myString.size();
int count = 0;
int val = 0;
int sum = 0;
char sign = ‘?’;
vector<int> arr;
size_t found = myString.find(sign);
if (found != string::npos)
{
eraseDemo(myString, found);
myString = myString;
x–;
}
while(count < x)
{
arr.push_back((int)myString[count] – 48);
count++;
}
int xx = arr.size()-1;
while(xx >= 0)
{
val = arr[xx] * 2;
if(val > 9)
{
val = val – 9;
}
replace(&val, &arr[xx]);
xx = xx – 2;
}
for(int ii = 0; ii < x; ++ii)
{
sum = sum + arr[ii];
}
return sum;
}
char Sum(string input)
{
int s = checkLuhn(input);
int ss = (s * 9) % 10;
char c = ss;
return c;
}
int main()
{
string x = “?942682966937054”;
char sign = ‘?’;
string result;
int mis = 0;
size_t found = x.find(sign);
mis = (checkLuhn(x) * 9) % 10;
Sum(x);
char s = ‘0’ + Sum(x);
replace2(&s , &x[found]);
cout << endl;
for(int xx = 0; xx < x.size(); xx++)
{
cout << x[xx];
}
cout << endl;
return 0;
}
Expert Answer
Answer to Write a c++ program that fixes a list of bank card numbers. Some of them just miss one of digits and you should restore …
OR