0

I'm currently working on Credit. I'm trying to find the first two digits of the card number using a for loop. I've created an array named firstdigits with two elements. Then I've created a for loop to assign the first two digits of the card number to the first and second elements of the array, respectively. However I keep getting an error message indicating the lines firstdigits[0] = card[z]; and firstdigits[1] = card[z]; which says 'subscripted value is not an array, pointer, or vector.' Could someone help, please?

#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
long long card = get_long_long();


int firstdigits[1];

for (int z = 0; z < 2; z++)
{
     if (z == 0)
    {
        firstdigits[0] = card[z];
    }

    if (z == 1)
    {
        firstdigits[1] = card[z];
    }
}

1 Answer 1

1

A simple way is a loop over a copy of the number. number % 10 returns the last digit, while number / 10 returns the number with last digit removed. Remove digits from the copy until it's less than 100 and you have your first two digits.

int firstdigits[1]; makes place for one integer at index 0. It doesn't have a firstdigits[1], that would require it to be at least of length 2.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .