Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

No idea why this is stopping

Name: Anonymous 2011-11-08 20:58

For some reason this seems to freeze up at line 48. Any idea why?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int validate(int dungus[], int dringus);
int checkLength(int dungus[], int dringus);

int main(){
    int a[16];
    int length, b;
    int x=0;
   
    printf("Enter each number of your credit card followed by enter.\n");
    printf("If your card has less than 16 numbers, type zero for the remaining slots.\n");
       
    while(x<16){
        scanf("%d",&a[x]);
        printf("x: %d-%d\n",x,a[x]);
        x++;
    }
   
    length = checkLength(a, 16);
    printf("Length: %d\n",length);
   
    if(validate(a, length)==1){
        printf("Valid\n");
    }
    else{
        printf("Not Valid\n");
    }
}

int validate(int dungus[], int dringus){
    int type=0;
    int valid=0;
    int math[7];
    int sum;
    int sum2;
    int sum3;
    int a=0;
   
    printf("What kind of credit card is this?\n");
    printf("Enter 1 for Visa, 2 for Master Card, 3 for AMEX, or 6 for Discover\n");
    scanf("%d",&type);
    printf("You chose %d:\n",type);
   
    // the program doesn't seem to reach the next printf statement for some reason.
    if(type==1){
        printf("I got this far...");
        if (dungus[0]==4){
            valid=1;
            printf("Confirmed as Visa");
        }
        else{
            return valid;
        }
    }
    else if(type==2){
        if (dungus[0]==5){
            valid=1;
            printf("Confirmed as Master Card");
        }
        else{
            return valid;
        }
    }
    else if(type==3){
        if ((dungus[0]==3)&&(dungus[1]==7)){
            valid=1;
            printf("Confirmed as AMEX");
        }
        else{
            return valid;
        }
    }
    else if(type==4){
        if (dungus[0]==6){
            valid=1;
            printf("Confirmed as Discover");
        }
        else{
            return valid;
        }
    }
    else{
        return valid;
    }
   
    if (dringus==16){
        math[0]=dungus[14]*2;
        math[1]=dungus[12]*2;
        math[2]=dungus[10]*2;
        math[3]=dungus[8]*2;
        math[4]=dungus[6]*2;
        math[5]=dungus[4]*2;
        math[6]=dungus[2]*2;
        math[7]=dungus[0]*2;
       
        while(a<=7){
            if(math[a]>9){
                math[a]=(math[a]/10)+(math[a]%10);
            }
            sum=sum+math[a];
        }
       
        a=0;
       
        sum2=dungus[15]+dungus[13]+dungus[11]+dungus[9]+dungus[7]+dungus[5]+dungus[3]+dungus[1];
        sum3=sum+sum2;
       
        if(sum3%10==0){
            valid=1;
        }
        else{
            valid=0;
        }
    }
    else if(dringus==13){
        printf("13 number credit cards not implemented yet. Please ignore validity status.\n");
        valid=1;
    }
    else{
        valid=0;
    }
   
    return valid;
}

int checkLength(int dungus[], int dringus){
    int a=0;
    int b=0;
   
    // this method didn't work for numbers with zero
    /*while(a<=dringus){
        if(dungus[a]!=0){
            b++;
        }
        a++;
    }*/
   
    if ((dungus[13]!=0)&&(dungus[14]!=0)&&(dungus[15]!=0)){
        return 16;
    }
    else{
        return 16;
    }
}

Name: Anonymous 2011-11-08 22:03

>>23
always print \n after a string or use fflush(stdout) after printf statement if you are using that printf for debugging purposes. stdout is line buffered when it prints to terminal. until buffer is full or you print \n, nothing will appear on screen and it will be buffered forever since your program hangs in an infinite loop

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List