Menu

Hi Following Error Luhn Algorithm Program Thread 1 Excbadaccess Code 1 Address 0x7ffeefc00 Q43803664

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

The program itself is supposed to take a line which says howmany cards need to be manipulated. A card can either be missing avalue, exemplified by the presence of ‘?’, or it has swap pairerrors (which is when a pair of values is swapped and make thecombination of values in the card wrong). The mentioned errorappears under the fixcard() function, with theswap(card[n],card[nn]); and under main() withfixCard(input[j]);

The program is written in c++, and I have tested the individualfunctions with hard coded string samples. Currently, the issueseems to be with the fixcard() function. When given an input ofsize n, only one output line followed by an exception is received.For the following sample, only the firstvalue  738119?7648…. is computed, and after this anexception 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(), ‘n’);

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;

}

Expert Answer


Answer to Hi! I am having the following error for my Luhn Algorithm program: Thread 1: EXC_BAD_ACCESS (code=1, address=0x7ffeefc00…

OR