vendredi 13 septembre 2019

How to compare string or char at the 'if else' statement?

I want to compare the user input's guessed cards with the cards that have been randomly picked, that is stored in the countCardsSuits[] and countCardsPips[]

I keep on getting the opposite output. Where it should be Congratulations!. Not the other.

I already tried using the strcmp() but it doesn't work either.

I declared:

int i, p, j, k, h, t, pips, suits; 
    int x = 3; 

    const char *countCardsPips[3];
    const char *storedCardsPips[3];

    const char *countCardsSuits[3];
    const char *storedCardsSuits[3];

    const char *wholePips[13] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    const char *wholeSuits[4] = {"D", "C", "H", "S"};

and the random:

srand(time(NULL));
            Sleep(t*100);
            printf("You have %d seconds to remember the cards!\n\n", t);

            for (i = 0; i<x; ++i) //looping until we get 3 cards from deck
                {

                    pips = wholePips[(rand()%13)];
                    countCardsPips[i] = pips;

                    suits = wholeSuits[(rand()%4)];
                    countCardsSuits[i] = suits;

                    printf("%3s %s\n", pips, suits);

                }

and the user input with compare:

do
                    {
                        for (j = 0; j<x; ++j)
                    {
                        k = j + 1;
                        printf("\nEnter the %d card's Pips and Suits:\n", k);
                        scanf("%s %s", &storedCardsPips[j], &storedCardsSuits[j]);
                    }

                if (countCardsPips[0] != storedCardsPips[0] && countCardsPips[1] != storedCardsPips[1]
                    && countCardsPips[2] != storedCardsPips[2] && countCardsSuits[0] != storedCardsSuits[0] &&
                    countCardsSuits[1] != storedCardsSuits[1] && countCardsSuits[2] != storedCardsSuits[2])
                {
                    printf("\nTry again!\n");
                    p = 1;
                }

                else
                {
                    printf("Congratulations!\n");
                    p = 0;
                }

            }
            while (p == 0);

I want when I entered the same cards as the randomly generated ones, I will get Congratulations! as output. And Try again! if I entered incorrectly.

Aucun commentaire:

Enregistrer un commentaire