Question
Write a c++ program that fixes a list of bank card numbers. Some of them just miss one of digits and you should restore this
Write a c++ program that fixes a list of bank card numbers. Some of them just miss one of digits and you should restore this digit. Others have a pair of adjacent digits swapped - you are to find the leftmost pair which, if swapped, makes valid card number.
All the card numbers would be 16 digits long.
Input data contain a list of workers who have their card numbers incorrect. Next lines contain a single card number each. If the number contains "?" (question mark) instead of some digit - this digit should be restored. If all digits are present, then "swap-error" should be fixed. Answer should contain a list of "fixed" card numbers.
input data:
4
?942682966937054 1217400151414995 2146133934?67114 2553514623364925
answer:
3942682966937054 1217040151414995 2146133934667114 2553514623369425
______________________________________________________________________________________
I need some help with my program. It is able to find a value to replace the "?" with, but it does not find the right value for all of the test cases above. I also wrote a function(not included in below code), which use if statement to branch when the string does not contain "?" and deal with swap errors. For that, my approach was to swap adjacent values, but I was able to get the right value output to match the output sample above. I believe a map would be helpful to deal with this program, but I am not familiar with map as data structures. I would appreciate your input. Since this is my first time trying to work with map, I would appreciate comments inside the code. Also, I use Luhn Algorithm to check if a card is valid or not.
---------------------------------------------------------------------------------------------------------------------------------------------------
#include
#include
#include
#include
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
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;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started