1

Im struggling with manipulating the string of numbers in the credit problem. I've figured out how to divide the initial input number by 10 (*100 each iteration) then mod % by 10 in order to access the 2nd to last digit, then every other digit following. However Im having trouble figuring out how to effectively store those outputs so that I can double them in order to do the check sum. Right now I have each one being transfered to its own variable in a series of 7 if statements within the loop, but I feel like there has to be a more efficient way to do so. (I also have it mapped to 7 if statements, and the loop repeating 7 times based on the sample number given, but since cc numbers can range from 14-16 characters I think that may cause problems down the road. I ideally would like int i to represent the number of figures in the input string but so far havent been able to find out how to take the length of a long long and assign that length to a variable. Is there a check length() function of some kind?)

Im also struggling with how to split the individual digits of a double digit number to add them (ie cc number =9876, take 7, double it to get 14, but Im trying to add 1+4 to my total rather than adding 14 to my total). I think I can do this by taking mod % 10 of that number then dividing it and taking mod % 10 again to get the separate digits, but since all of my outputs are in individual variables I cant figure out how to set that up without an additional series of 7 if statements. I also then run into the trouble of how to link those two mod and divide functions without assigning their outputs to seperate variables again.

I feel like there is either some sort of operation that I may be missing, or an aspect to the operations Im using that I am not understanding/implementing correctly. I've pasted my code below in case that would help with figuring out where I may be going wrong.`enter code here

#include <cs50.h>
#include <stdio.h>

int b, c, d, e, f, g, h;

int main(void)
{
long long x = get_long_long();  //gets credit card number input
long y = 10;   //is the value that the input will be mod'ed by
long long a = 10; //is the divisor so that mod 10 gives every other digit from 2nd to last

for (int i = 0; i<7;i++)  //repeates 7 times to get every other digit in the sample cc number 
{
    int z = (x/a) % y;  //z is the output, the every other digit so to speak
    printf("%lld divided by %lld mod %ld equals %i\n", x, a, y, z); //just for testing to see values
        if (a==10) //this is a long winded way Ive figured out how to seperate the digits to their own 
                    //values so that they can be multiplied by 2 individually
        {
           b=z*2;
        }
        if (a==1000)
        {
           c=z*2;
        }
        if (a==100000)
        {
           d=z*2;
        }
        if (a==10000000)
        {
           e=z*2;
        }
        if (a==1000000000)
        {
           f=z*2;
        }
        if (a==100000000000)
        {
           g=z*2;
        }
        if (a==10000000000000)
        {
           h=z*2;
        }
    a*=100; //this increments so that the value given by the mod changes by 2 places each loop
}
long long sum_of_doubled=b+c+d+e+f+g+h; //this gives the sum of the digits aquired above
printf("%i, %i, %i, %i, %i, %i, %i \n", b,c,d,e,f,g,h ); //shows the every other digits once doubled
printf("%lld is the sum of every other digit\n", sum_of_doubled); //just to show value during testing

}
1
  • You might want to listen to the week 2 lectures first and then come back to this problem. It presents array data structures, which will make your life much easier. You will see that many assignments there, especially those for more comfortable, go slightly ahead of the curriculum, sort of forcing you to invent a bicycle and then learn about the bicycle.
    – AnneB
    Jun 2 '17 at 0:52

You must log in to answer this question.

Browse other questions tagged .