Pinpoint each digit within a given number of any length

Viewed 27

I am trying to find each digit of a number entered by the user. The length of the number is up to the user.

So far I have:

  • managed to limit what the user enters by using long long card, meaning they cannot enter more than about 18 or 19 characters and only numbers are accepted.
  • managed to get the first and last digit of that number, as well as the count of the number.

Now I wanted to:

  1. specifically ask for no more than 16 digits max - > but it keeps saying undeclared identifier and am not sure where I go wrong
  2. be able to identify each digit, i.e.:
    53689
    1st digit:5
    2nd digit:3
    3rd digit:6
    etc.
    

I learned about modulus but I can't figure out how to tell it to stop at the 2nd last/3rd last digit. I feel it's something really simple but how? especially if I don't know if the user enters 5 numbers or 15, otherwise I could say (repeat modulus x-1 times) or something like that.

Just for now, my code in case you see anything for #1. Before I tried to limit the entry-conditions, it worked fine showing me card, card length and 1st,last digit.

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

//finding first 2 numbers and last number and checking what card type


int main(void)
{
int count = 0;
do
{
    long long card = get_long_long("Enter your card number: ");
}
while (card ! = 0)
{
    card = card / 10;
    count++;
}

    printf("Card number: %llu\n", card);
    printf("Number of digits: %d \n", count);

firstdigit = card;

while (firstdigit >=  20)
{
    firstdigit = firstdigit / 10;
}

printf("First digit = %d \n", firstdigit);

lastdigit = card % 10;
    printf("Last digit= %d \n", lastdigit);

    return 0;
 }

info: Yes it is from CS50 credit, I can't understand the full exercise yet so I decided to take it apart and only learn how to write a program that analyses the number for first 2 digits and tells me what card type it is. To do so I'm trying to learn how to count every number on any given number length for practice, for me to actually understand this slowly, so please don't share a full answer to the credit problem.

I also haven't learned arrays or more complex solutions yet either.

1 Answers

As others have advised, "break the problem down into smaller pieces".

Here's an example of that approach. Instead of dealing with a massive number all at once, this 'segments' the number into the typical 4 sections embossed into a credit card. This might give you a start toward a solution...

int main() {
    int count = 0;

    long long card = get_long_long("Enter your card number: ");
    long long cpy = card;

    int blk4 = cpy % 10000; // 4 rightmost digits as int
    cpy = cpy / 10000; // shift right

    int blk3 = cpy % 10000; // next 4 digits as int
    cpy = cpy / 10000; // shift right

    int blk2 = cpy % 10000; // next 4 digits as int
    cpy = cpy / 10000; // shift right

    int blk1 = cpy % 10000; // leftmost 3-4 digits as int

    printf( "Card number: %04d %04d %04d %04d\n", blk1, blk2, blk3, blk4 );

    return 0;
}

Having shown that, please be advised that a credit card "number" is not a "number" in the usual sense. It is a string made-up only of digits. Trying to solve this CV50 problem with "integers" is going to lead to tears. It's time to learn about arrays of characters (even if every character is an ASCII digit)...

Related