1

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));
}
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Duck Ling
  • 1,577
  • 13
  • 20
  • 1
    There's a [cs50 stack exchange site](https://cs50.stackexchange.com/) if you're interested. – pmg Dec 20 '18 at 15:25
  • 1
    Read the input as string, not as `long long` ! It will spare you from all of this huge numbers mess! And will give you the digits for (almost) free. – Eugene Sh. Dec 20 '18 at 15:36
  • OT: for ease of readability and understanding: consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. Suggest each indent level be 4 spaces. – user3629249 Dec 20 '18 at 17:12
  • Possible duplicate of [C passing variable by reference](https://stackoverflow.com/questions/13069337/c-passing-variable-by-reference) – Govind Parmar Dec 20 '18 at 20:22

1 Answers1

2

Your function multiply2 changes the value of the passed parameter only in case its value is at least 5.
Try this:

void multiply2(int* pX)
{
  *pX = *pX * 2; /* Multiply value by 2 */
  if (*pX >= 10)
    *pX = (*pX % 10) + 1; /* Adjust if value greater than or equal to 10 */
  printf("%d\n", *pX);
}
Robert Kock
  • 5,795
  • 1
  • 12
  • 20
  • Thanks Robert. This is actually how it looked like before I started fiddling with pointers, but that's not the main issue. the issue is - the values of ccn1 to ccn8 don't update, because they're just changed within the function. I'd like them to change to the values they gain within the function multiply2. basically after they're being checked (>= 10) and then changed (multiplied by 2) I'd like the value of pX to be transferred as ccn1, then ccn2, etc – Duck Ling Dec 20 '18 at 15:43
  • This is exactly what happens. For example: `int x=3; multiply2(&x);` After the function call, the value of x is 6. – Robert Kock Dec 20 '18 at 15:57
  • oh my gosh! such a silly mistake! also in summary of ccn1 to ccn16 I've missed ccn15. I'm sorry for bothering you about such a small detail. Thanks very much Robert :) – Duck Ling Dec 20 '18 at 16:12