I have a problem with the function 'multiply2' which doesn't change values of ccn1 to ccn8. It has to multiply them by 2, and if the result is equal or more than 10, than take products of this equation and add them together (i.e. 7 * 2 = 14, so 1 + 4 = 5). The whole thing would work, but I've noticed that the function 'multiply2' keeps the value of pX to itself, and I'd like to overwrite values of ccn1 to ccn8, and then sum all values together (ccn1 to ccn16). Please help :( PS: I'm new at this so please be gentle, I'm sure it looks like a mess to you.
The program compiles as it is with no errors, mind you, it does include cs50.h library for "get_long_long" function which prompts a user for a credit card number
I've tried to use pointers but clearly I don't fully understand the concept! I've managed to dereference *pX which assigned values of ccn1, ccn2, etc. but they still don't update in main.
the whole problem described here: https://docs.cs50.net/2018/x/psets/1/credit/credit.html#tl-dr
Thank you!
# include <cs50.h>
# include <stdio.h>
# include <math.h>
void multiply2 (int * pX);
long long ccn;
//a program to check the credit card number and print out if it's American Express, Visa, or MasterCard. if else - ILVALID.
int main(void)
{
//prompt user for a credit card number
ccn = get_long_long("Number: ");
//every second digit starting from second to last
int ccn1 = (ccn % 100) / 10;
int ccn2 = (ccn % 10000) / 1000;
int ccn3 = (ccn % 1000000) / 100000;
int ccn4 = (ccn % 100000000) / 10000000;
int ccn5 = (ccn % 10000000000) / 1000000000;
int ccn6 = (ccn % 1000000000000) / 100000000000;
int ccn7 = (ccn % 100000000000000) / 10000000000000;
int ccn8 = (ccn % 10000000000000000) / 1000000000000000;
//printf("%i\n%i\n%i\n%i\n%i\n%i\n%i\n%i\n", ccn1, ccn2, ccn3, ccn4, ccn5, ccn6, ccn7, ccn8);
//all the other digits
int ccn9 = (ccn % 10);
int ccn10 = (ccn % 1000) / 100;
int ccn11 = (ccn % 100000) / 10000;
int ccn12 = (ccn % 10000000) / 1000000;
int ccn13 = (ccn % 1000000000) / 100000000;
int ccn14 = (ccn % 100000000000) / 10000000000;
int ccn15 = (ccn % 10000000000000) / 1000000000000;
int ccn16 = (ccn % 1000000000000000) / 100000000000000;
//printf("%i\n%i\n%i\n%i\n%i\n%i\n%i\n%i\n", ccn9, ccn10, ccn11, ccn12, ccn13, ccn14, ccn15, ccn16);
if (((ccn >= 340000000000000 && ccn <= 349999999999999) || (ccn >= 370000000000000 && ccn <= 379999999999999)) ||
((ccn >= 5100000000000000 && ccn <= 5199999999999999) || (ccn >= 5500000000000000 && ccn <= 5599999999999999)) ||
((ccn >= 4000000000000 && ccn <= 4999999999999) || (ccn >= 4000000000000000 && ccn <= 4999999999999999)))
{
multiply2(&ccn1);
multiply2(&ccn2);
multiply2(&ccn3);
multiply2(&ccn4);
multiply2(&ccn5);
multiply2(&ccn6);
multiply2(&ccn7);
multiply2(&ccn8);
int sum = (ccn1 + ccn2 + ccn3 + ccn4 + ccn5 + ccn6 + ccn7 + ccn8 + ccn9 + ccn10 + ccn11 + ccn12 + ccn13 + ccn14 + ccn16);
printf("%i\n", sum);
}
/NOTHING FROM ABOVE - INVALID
else
{
printf("INVALID\n");
}
}
void multiply2 (int * pX)
{
if ((*pX *2) >= 10)
{
*pX = ((*pX * 2) % 10) + 1;
printf("%i\n", *pX);
}
else
{
printf("%i\n", (*pX * 2));
}
}