Reputation: 39
I'm taking a programming class in C, and I've run into a bug I can't solve no matter how hard I try.
The program instructs me to: "Ask the user for the first 12 digits of an EAN number, then calculate the 13th "security" digit by performing the following operations:
Add the second, fourth, sixth, eighth, tenth, and twelfth digits Add the first, third, fifth, seventh, ninth, and eleventh digits Multiply the first sum by 3 and add it to the second sum Subtract 1 from the total Compute the remainder when the adjusted total is divided by 10 Subtract the remainder from 9
I'm coming from Python, so the easiest way to split the number into it's individual digits is to convert it to a string and then revert each element in the character array to an int stored in an integer array.
Here's my code so far:
#include <stdio.h>
int main(void)
{
long ean; //creates variable to store a 12 digit integer
int digits[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; //initializes a 1x12 integer array
printf("Please enter the first 12 digits in your EAN: ");
scanf_s("%ld", &ean); //collects the appropriate user input and stores it in ean
char seperated[12]; //creates character array
**sprintf_s(seperated, "%ld", ean); //converts the long integer ean to a string called separated
for (int i = 0; i < 12; i++)
{
sscanf_s(seperated[i], "%d", &digits[i]); //iterates through the characters and converts each one to an integer
//stored in the array created earlier
printf("%d\n", digits[i]);
}
int sumEvens = digits[1] + digits[3] + digits[5] + digits[7] + digits[9] + digits[11]; //sums the even digits
int sumOdds = digits[0] + digits[2] + digits[4] + digits[6] + digits[8] + digits[10]; //sums the odd digits
int total = (3 * sumEvens + sumOdds)-1; //multiplies the first sum by 3 and adds it to the second sum,
//then subtracts one from the total
int securityDigit = 9 - (total % 10); //Computes the remainder when the total is divided by 10 and subtracts that from 9
return 0;
}
The problem is, my sprintf statement (marked with **) is throwing an exception saying
Exception thrown at 0x0FB7373A (ucrtbased.dll) in ECE1400_2.exe: 0xC0000005: Access violation reading location 0xBE991A6D.
Does anyone know what I'm doing wrong and how I can fix it? I hope it's just something little, but I've been staring at this and researching for well over 2 hours.
Upvotes: 0
Views: 298
Reputation: 46
there's a lot of unnecessary part.
int main()
{
char s[12] = {0};
int n = 0;
int digits[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; //initializes a 1x12 integer array
printf("Please enter the first 12 digits in your EAN: ");
for (int i = 0; i < 12; i++)
{
scanf("%d", &digits[i]);
}
int sumEvens = digits[1] + digits[3] + digits[5] + digits[7] + digits[9] + digits[11]; //sums the even digits
int sumOdds = digits[0] + digits[2] + digits[4] + digits[6] + digits[8] + digits[10]; //sums the odd digits
int total = (3 * sumEvens + sumOdds)-1; //multiplies the first sum by 3 and adds it to the second sum,
//then subtracts one from the total
int securityDigit = 9 - (total % 10); //Computes the remainder when the total is divided by 10 and subtracts that from 9
for (int i = 0; i <= 11; i++) {
n += sprintf (&s[n], "%d", digits[i]);
}
}
Upvotes: 2
Reputation: 50778
This would be a start:
int main(void)
{
__int64 ean; // using 64 bit integer
int digits[12] = { 0 }; // one zero is enough
printf("Please enter the first 12 digits in your EAN: ");
scanf_s("%lld", &ean); // using %lld here because ean is a 64 bit type
char seperated[12];
sprintf_s(seperated, 12, "%lld", ean); // also using %lld
for (int i = 0; i < 12; i++)
{
digits[i] = seperated[i] - '0'; // I let you find out yourself about this "magic"
printf("%d\n", digits[i]);
}
...
Upvotes: 0
Reputation: 123
You probably want to do something like this
int main(void)
{
int a[5]={1,2,3,1,3};
char s[9] = {0};
int n = 0;
for (int i = 0; i < 5; i++) {
n += sprintf (&s[n], "%d", a[i]);
}
printf ("\n char* s = %s\n\n", s);
return 0;
}
instead of making it long.
and there's no need for you to use sscanf_s as mentioned in the comments.
Upvotes: 1