Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi! I am having the following error for my Luhn Algorithm program: Thread 1: EXC_BAD_ACCESS (code=1, address=0x7ffeefc00000) The program itself is supposed to take a

Hi! I am having the following error for my Luhn Algorithm program: Thread 1: EXC_BAD_ACCESS (code=1, address=0x7ffeefc00000)

The program itself is supposed to take a line which says how many cards need to be manipulated. A card can either be missing a value, exemplified by the presence of '?', or it has swap pair errors (which is when a pair of values is swapped and make the combination of values in the card wrong). The mentioned error appears under the fixcard() function, with the swap(card[n],card[nn]); and under main() with fixCard(input[j]);

The program is written in c++, and I have tested the individual functions with hard coded string samples. Currently, the issue seems to be with the fixcard() function. When given an input of size n, only one output line followed by an exception is received. For the following sample, only the first value 738119?7648.... is computed, and after this an exception is thrown. FYI, I am using Xcode.

Example of sample input which failed:

43

738119?764880623

5254204853319164

16967140390609?5

357?189348513114

?227582517054559

8617147935349583

2379302237648450

217207?651989954

159084306605?878

5100522296934574

560130?005740906

64?0808960965410

92689458960?6481

93172?5399134834

4?12364930381398

6542542442996115

6168653656960804

4858442518083020

4?59075650405755

5169865596582266

1309006586675?88

3899?55416456492

22?8182428141737

5363557160982123

26304598721?3605

11?6000265713099

3588751618063904

?671012965135063

209321517271?950

626?443323174511

6951506702796248

43835?2775464882

3079?82610231668

8465?76602306446

8448601828713698

1330958955734738

49991676?9132593

8012758469304814

6468525847181430

8831503?63794686

4215166584824623

72999243?0218283

8686998586685266

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

#include

#include

#include

#include

#include

using namespace std;

void replace(char* a, char* b)

{

*b = *a;

}

int digitSum(int num)

{

return num/10 + num%10;

//or return num - 9;

}

int findSwapError(string strRepair, int posOfSign)

{

int len = strRepair.length();

string strEven, strOdd;

int evenSum = 0, oddSum= 0;

int hiddenDigit = 0, finalSum = 0;

for(int i = 0; i < len; i++)

{

if(i%2 == 0 && strRepair[i] != '?')

{

evenSum = evenSum + digitSum((strRepair.at(i) - 48) * 2);

}

else if(strRepair.at(i) != '?' && i%2 != 0)

oddSum = oddSum + strRepair.at(i) - 48;

}

if(posOfSign%2 != 0)

{

finalSum = evenSum + oddSum;

hiddenDigit = (finalSum * 9)%10;

}

else

{

finalSum = evenSum + oddSum;

hiddenDigit = (finalSum * 9)%10;

if(hiddenDigit%2 == 0)

{

hiddenDigit = hiddenDigit/2;

}

else

{

switch(hiddenDigit)

{

case 1 : hiddenDigit = 5;

break;

case 3 : hiddenDigit = 6;

break;

case 5 : hiddenDigit = 7;

break;

case 7 : hiddenDigit = 5;

break;

case 9 : hiddenDigit = 9;

break;

}

}

}

return hiddenDigit;

}

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)

{

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();

cin.ignore(numeric_limits::max(), ' ');

vector input;

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

{

input.push_back(x);

s++;

}

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;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 22nd European Conference Adbis 2018 Budapest Hungary September 2 5 2018 Proceedings Lncs 11019

Authors: Andras Benczur ,Bernhard Thalheim ,Tomas Horvath

1st Edition

3319983970, 978-3319983974

More Books

Students also viewed these Databases questions

Question

why we face Listening Challenges?

Answered: 1 week ago

Question

what is Listening in Context?

Answered: 1 week ago