1

I am confused, when i use array values to assign another array values. the original array deletes the values used

int main(int argc, char *argv[]) {

    char original[ORIGINAL_SIZE];
    int isbn[ISBN_SIZE];
    int index = 0;
    int code;
    int weight = 10;
    int weightedValue;
    int weightedSum = 0;

    printf("Enter an ISBN to validate: ");
    validateISBNArray(original);

    while(index < ORIGINAL_SIZE){
        if(original[index] != '-'){
            if(original[index] == 'x' || original[index] == 'X') isbn[index] = 10;
            else if(original[index] == 0) isbn[index] = 0;
            else isbn[index] = original[index]-48;
            code = isbn[index];
            //printf("%d", code);
            weightedValue= code*weight;
            weight--;
            weightedSum += weightedValue;
        }
        index++;
    }
    printf("%s",original);
    if(weightedSum%11==0) printf("The ISBN %s, is VALID", original);
    else printf("The ISBN %s, is NOT VALID", original);

    return 0;
}

validateISBNArray has no effect on original array

this is the code for the not affecting function

void validateISBNArray(char array[]){

    int index = 0;
    int countDigits = 0;
    int value = 0;  

    clearArray(array, ORIGINAL_SIZE);
    scanf("%s",array);      

    while(index < ORIGINAL_SIZE){
        //printf("%d %c %d\n", index, array[index], countDigits);
        if(((array[index]-48) >= 0 && (array[index]-48) <= 9) || (array[index] == 'x'|| array[index] == 'X')) {
            countDigits++;
            index++;
        }
        else if(array[index] == '-' || array[index] == 0) index++;
        else{
            printf("INVALID CHARACTER %d = %c. Please Enter Digits Or/And Hyphens Only: ", index, array[index]);
            index = 0;
            countDigits = 0;
            clearArray(array, ORIGINAL_SIZE);
            scanf("%s",array);
        }
        if(index == ORIGINAL_SIZE && countDigits != 10){
            printf("INVALID NUMBER OF DIGITS %d. Please Enter 10 Digits: ", countDigits);
            index = 0;
            countDigits = 0;
            clearArray(array, ORIGINAL_SIZE);
            scanf("%s",array);
        }       
    }
    //printf("%s", array);
}
9
  • Please post a minimal reproducible example. We don't know what validateISBNArray() does. Commented Apr 29, 2017 at 17:19
  • as stated it has no effect on original array i will post it anyways Commented Apr 29, 2017 at 17:20
  • You index isbn[index] up to ORIGINAL_SIZE although isbn is declared as int isbn[ISBN_SIZE];. Are you sure this is safe? Commented Apr 29, 2017 at 17:27
  • you can probably use a debugger - then you might find out the problem with your code yourself. Commented Apr 29, 2017 at 17:27
  • Are you saying that, say, clearArray() does not affect the original array? Hmmm... Commented Apr 29, 2017 at 17:30

1 Answer 1

1

Ok I fixed it removing the need for the isbn array

int main(int argc, char *argv[]) {

    char original[ORIGINAL_SIZE];
    int isbn[ISBN_SIZE];
    int index = 0;
    int code;
    int weight = 10;
    int weightedValue;
    int weightedSum = 0;

    printf("Enter an ISBN to validate: ");
    validateISBNArray(original);

    while(index < ORIGINAL_SIZE){
        if(original[index] != '-'){
            if(original[index] == 'x' || original[index] == 'X') code = 10;
            else if(original[index] == 0) code = 0;
            else code = original[index]-48;
            weightedValue = code*weight;
            weight--;
            weightedSum += weightedValue;
        }
        index++;
    }
    if(weightedSum%11==0) printf("The ISBN %s, is VALID", original);
    else printf("The ISBN %s, is NOT VALID", original);

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.