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

Pages: 1-

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 20:59

And before someone gets on my case, yes, this is my homework, and I am asking for help with it.

Name: THE SUPER JEW 2011-11-08 21:06

Maybe because it's expecting some kind of input?

Name: Anonymous 2011-11-08 21:08

Halting problem.

Name: Anonymous 2011-11-08 21:08

>>3
Doesn't seem to be the problem. I just stops right at line 48 for no reason at all. The value of type is definitely one, but for some reason it doesn't move past that if statement, or anything in it.

Name: Anonymous 2011-11-08 21:09

1. Use proper white spacing.
2. Use constants. example:

    const int VISA = 1;
    const int MASTER_CARD = 2;

3. Give meaningful variable names.
4. Your main() function is supposed to return some value.
5. Your checkLength() function is not returning a value.
6. ?????
7. IHBT

Name: Anonymous 2011-11-08 21:09

>>4
What exactly does that mean? This is my first year and I've never had this issue in the other languages I've tinkered with.

Name: Anonymous 2011-11-08 21:10

>>6
>5. Your checkLength() function is not returning a value.
Its returning 16.

Name: Anonymous 2011-11-08 21:10

flush your stdio. maybe there's something left in there because you're using the goddamn scanf()

Name: Anonymous 2011-11-08 21:11

>>8
check again.
it's inside the else{}

Name: Anonymous 2011-11-08 21:11

>>6
2. Use Constants. example:
const int VISA = 1;
const int MASTER_CARD = 2;

Or don't be a retard and use a fucking enum.

Name: Anonymous 2011-11-08 21:12

>>6
Thanks for the advice. I'm not sure about #5 and #1 though. What constitutes 'proper white spacing' and last I checked checkLength is returning 16.

Name: Anonymous 2011-11-08 21:14

>>10
...and? It still returns 16.
>>9
You're probably right. I've noticed NOBODY but my teacher uses it, so I assumed there must be something wrong with using it in practice. I'll try to avoid using it outside of class, but I think its best I follow his example for now.

Anyone else?

Name: Anonymous 2011-11-08 21:26

So I went ahead and tried flushing stdio, but it didn't seem to do anything. To elaborate on my problem, I'm typing in a valid Visa credit card number, and choosing 1 for Visa at the prompt. The last statement that is visibly executed is

printf("You chose %d:\n",type);

...and that outputs 1, which is correct.

Anyone see something I don't?

Name: Anonymous 2011-11-08 21:36

    if ((dungus[13]!=0)&&(dungus[14]!=0)&&(dungus[15]!=0)){
        return 16;
    }
    else{
        return 16;
    }

Name: Anonymous 2011-11-08 21:39

it prints 1 for type but does not print "I got this far..."?

Name: Anonymous 2011-11-08 21:39

>>15
Again, you get 16 either way. 16 is being returned. Also, I went ahead and fixed the first half so it returns 13. If you need my updated code to see that checkLength is not the problem, here you go.


#include <stdio.h>
#include <stdlib.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++;
    }
    a[16]=0;
   
    x=0;
   
    while(x<16){
        printf("%d",a[x]);
        x++;
    }
   
    printf("\n");
   
    x=0;
   
    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); // this is the last visibly executed statement
   
    // the program doesn't seem to reach the next printf statement for some reason.
    if(type==1){
        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)&&(dungus[16]==0)){
        return 13;
    }
    else{
        return 16;
    }
}

Name: Anonymous 2011-11-08 21:40

>>16
Exactly. No idea what's wrong.

Name: Anonymous 2011-11-08 21:42

don't tell me you are using dev c++

Name: Anonymous 2011-11-08 21:49

>>19
gedit and gcc. Sorry to disappoint you.

Name: Anonymous 2011-11-08 21:53

So I'm guessing all of you guys are lost too...

Name: Anonymous 2011-11-08 21:55

>>20

        while(a<=7){
            if(math[a]>9){
                math[a]=(math[a]/10)+(math[a]%10);
            }
            sum=sum+math[a];
        }

you missed a++. it does not print "you have got this far" because it is buffered and it loops in that while loop

Name: Anonymous 2011-11-08 21:59

>>22
That seems to have fixed that problem. I'm having a hard time grasping how something farther along in the program effects that printf statement. Any way to avoid something like this happening in the future, other than remembering to type a++?

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

Name: Anonymous 2011-11-08 22:03

By the way, why does main need to return something? Is there anything wrong with it being a void?

Name: Anonymous 2011-11-08 22:04

>>24
Thanks!

Name: Anonymous 2011-11-08 22:05

>>25
that return value is important to the Operating System (ie. Linux)

Name: Anonymous 2011-11-08 22:06

>>27
Interesting. I've never had any problems in the past with void though. What kind of strange occurrences can it cause?

Name: Anonymous 2011-11-08 22:08

>>28
Not much. It's important to other programs that might use your program. If it returns 0, then nothing is wrong. If it returns something else, then shit is about to break loose.

Name: Anonymous 2011-11-08 22:10

>>28
and you declare it as int main()
which means that it really is supposed to return something.

Name: Anonymous 2011-11-08 22:13

>>28
probably nothing, but some OSes might say something like "this program terminated incorrectly" etc since your main does not actually returns anything it returns a garbage value

Name: Anonymous 2011-11-08 22:25

Another question: Why am I getting crazy results from

while(a<=7){
            if(math[a]>9){
                math[a]=(math[a]/10)+(math[a]%10);
            }
            sum+=math[a];
            a++;
        }

Its giving me things like 4915717 as a result, when the elements math[] are nothing but single digit values.

Name: Anonymous 2011-11-08 22:27

>>32
\n ?
overflow?

Name: Anonymous 2011-11-08 22:30

Set sum to zero in its declaration and it fixed itself. No idea why it wasn't zero to begin with...
>>33
I also got rid of some printf statements I was using for debugging purposes... That could've played a role in it I guess. I'm new to C, so I can't say for sure.

Name: Anonymous 2011-11-08 22:34

>>34
No idea why it wasn't zero to begin with...
They don't need to be. some compiler initializes them as 0 but that is not standard. if you don't initilaize a variable it will be random data

Name: Anonymous 2011-11-08 22:43

>>35
The strange thing is, all of the other int variables were 0. Only the this one was different...

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