Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

i'm coding a poker game for irc. This is a piece of the code, it prints the array of cards, and then the value of each card. The array is printed correctly, but a segment violation appears before the value of cards is printed.

Can someone help me with this? Suggestions for optimize the code are accepted too ;P

void barajar() {
srand(time(NULL));

for(i = 0 ; i < 52 ; i++) {
    cartas[i] = i;
}

for(i = 0 ; i < 100 ; i++) {
    int i1 = rand()%52;
    int i2 = rand()%52;
    int tmp = cartas[i1];
    cartas[i1] = cartas[i2];
    cartas[i2] = tmp;
}

for(i = 0 ; i < 51 ; i++) {
    printf("%d, ", cartas[i]);
}
i++;
printf("%d.\n", cartas[i]);
}


int main() {
barajar();
int olol;
for ( olol = 0; olol <=52; olol++ ) {
    int numero = cartas[c];
    int cnum = numero % 13;
    int numero1;

    switch(cnum) {
    case 0:
        nombre1 = "As";
        break;
    case 10:
        nombre1 = "J";
        break;
    case 11:
        nombre1 = "Q";
        break;
    case 12:
        nombre1 = "K";
        break;
    default:
            numero1 = cnum + 1;
        break;
    }
    int opera = numero / 13;
    switch ( opera ) {
        case 0:
            nombre2 = "diamantes";
        case 1: 
            nombre2 = "diamantes";
        case 2:
            nombre2 = "corazones";
        case 3:
            nombre2 = "picas";
        case 4:
            nombre2 = "espadas";
    }
    if (nombre1 != "\0") {
        strcat(carta, nombre1);
        strcat(carta, " de ");
        strcat(carta, nombre2);
        printf("%s", carta);
    }
    else {
        sprintf(carta, "%s", numero1);
        strcat(carta, " de ");
        strcat(carta, nombre2);
        printf("%s", carta);
    }
    c++;
}
}
share|improve this question
i changed if (nombre1 != "\0") for strcmp() and sprintf(carta, "%s", numero1); for %d. No clang/gcc errors, but the segmentation fault continues. – xacobe97 Dec 24 '12 at 0:54
The code seems to be missing some parts. What you posted does not compile. – William Morris Dec 24 '12 at 1:02
Hello, and welcome to CodeReview. As CR is for reviewing working code though, you'll probably have better luck with getting help on StackOverflow as it's considered off topic here. – Corbin Dec 24 '12 at 2:40
We would be more than willing to help you. But we only review working code (stackoverflow is for code that you need help fixing). But it definitely helps in the processes if we can compile and run your code. – Loki Astari Dec 24 '12 at 3:23
Oh, ok thanks, i'll post this in SO. And the only missing in the code are the includes and global variables. Thanks again – xacobe97 Dec 24 '12 at 9:48
show 1 more comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.