0

I've done this procedure but the program only asks one time the Input credit card digit group #%d, when I put that it has to do it 4 times. So I guess the problem is the comparison array[i] > 999 && array[i] < 9999 how can I change it so it works?

void demanarDigits(int i, int array[MAX]) {
    for (i = 0; i < 4; i++) {
        printf("Input credit card digit group #%d: ", i + 1);
        scanf("%d", &array[i]);
        if (array[i] > 999 && array[i] < 9999) {
            i = i + 1;
        }
        while (array[i] > 9999 || array[i] < 1000) {
            printf("ERROR: Digit groups must have 4 digits\n\n");
            printf("Input credit card digit group #%d: ", i + 1);
            scanf("%d", &array[i]);
            if (array[i] > 999 && array[i] < 9999) {
                i = i + 1;
            }
        }
    }
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Laia
  • 1
  • 1
  • I see you can increment the index variable `i` twice in the input loop. – Weather Vane Nov 24 '21 at 16:40
  • You ignore the helpful return value of scanf at your own risk. – Yunnosch Nov 24 '21 at 16:40
  • What is your goal with `if (array[i] > 999 && array[i] < 9999) { i = i + 1;}`? Can you describe the purpose of that in English? Maybe something like "contunue the loop if i is in range"? Similar for the second occurrence of `i = i + 1;`. – Yunnosch Nov 24 '21 at 16:44
  • 1
    Generally when dealing with data that can contain leading zeros (like phone or credit card numbers) it's often better to read and store them as strings. – Some programmer dude Nov 24 '21 at 16:46
  • The task isn't clear: are you trying to input 4 groups of 4 digits: taking them 4 at a time as single number? Rewinding, if you want to input a card number, do that as a single string (and keep it as a string). – Weather Vane Nov 24 '21 at 16:47
  • 1
    Please provide a [mre] which demonstrates your problem. And define the input which triggers the behaviour you describe, And I am not sure that you do actually describe the misbehaviour, or it is just me not getting it. Maybe spend a few more words on describing in either case. – Yunnosch Nov 24 '21 at 16:49
  • N ot ananswer to your question, but its generally better to treat credit card numbers as strings throughout and not convert them to numbers. This not only makes it easier to compare, but you can then also use regular expressions to determine whether a card number is correct or of a particular type. – ChrisBD Nov 24 '21 at 16:57

3 Answers3

0

I think you have to put the array argument of the function like this:

void demanarDigits(int i, int array[])

You mustn't put the index.

  • 1
    Using a size or not doesn't matter (in this case), the compiler will treat the argument as a pointer any way (i.e. both `int array[MAX]` and `int array[]` is equivalent to `int *array)`. – Some programmer dude Nov 24 '21 at 16:58
0

At least this problem

Off-by-1.

To test a 4 digit number in th [1000-9999] range, I'd expect

// if (array[i] > 999 && array[i] < 9999) {
if (array[i] > 999 && array[i] <= 9999) {
// or better as 
if (array[i] >= 1000 && array[i] < 10000) {

To test for a 4 digit input in the range [0-9999], use "%n" to record scanning offset.

// scanf("%d", &array[i]);
int start, end;
if (scanf(" %n%d%n", &start, &array[i], &end) == 1) {
  if (end - start == 4 && d >= 0) Good();

Still, I think using fgets() to read the input line and convert it to a string is best.

char buf[100];
if (fgets(buf, sizeof buf, stdin)) }{
  // Now parse buf[]
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

Hello I think this will work for you.

#include<stdio.h>

int demanarDigits (int array)
{
    if (array > 9999 || array < 1000)
    {
        printf("ERROR: Digit groups must have 4 digits\n");
        return 0;
    }
    else
        return 1;
}

int main ()
{
    int array[4],a;
    for (int i=0; i<4; i++)
    {
        printf ("Input credit card digit group #%d: \n", i + 1);
        scanf ("%d",& array[i]);
        a= demanarDigits (array[i]);
        if (a==0)
            i--;
    }
    return 0;
}