Thread: Strange error when running code

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    2

    Exclamation Strange error when running code

    Hello,
    I am having a trouble trying to run my program. I was trying to make a program stimulating poker game (just 1 of the exercises in the book I am reading). I had finished the first program to deal 5 cards to one player and determine some of the hand combinations. Then, I tried to modify it so that it can work for 2 players.

    This was when the problem began. Every time I run the modified code, the old code still run instead of the new code. This is the picture where I am so frustrated and delete almost of it, but the result is still the same (picture: In the picture, the running code is compiled from the code background).


    Strange error when running code-code-jpg


    This is my code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define MAXC 5 /* define number of cards on your hand here */
    #define PLAYER 2 /* define number of players */
    
    /* prototype */
    void shuffle (int a[][13]);
    void deal (const int a[][13], const char *b[], const char *c[], const int player);
    void record (const int dDeck[][13], int hSuit[], int hFace[], const int player);
    int pair ( const int hFace[] );
    int flush ( const int hSuit[] );
    int straight ( const int hFace[] );
    
    
    int main (void)
    {
        // Initialize face, suit and hand arrays
        const char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};
        const char *face[13] =     {"Ace", "Two", "Three", "Four",
                                    "Five", "Six", "Seven", "Eight",
                                    "Nine", "Ten", "Jack", "Queen", "King"};
        int deck [4][13] = {0};
        int handSuit1[MAXC] = {0};
        int handFace1[MAXC] = {0};
        int handSuit2[MAXC] = {0};
        int handFace2[MAXC] = {0};
        int *playerSuit [2] = {handSuit1, handSuit2};
        int *playerFace [2] = {handFace1, handFace2};
        
        // Shuffle and deal
        shuffle (deck);
        
        for (int i=0; i<=PLAYER-1; i++)
        {
            printf ("%s%d%s", "Player ", i+1, ":\n");
            deal (deck, suit, face, i);
            record (deck, playerSuit[i], playerFace[i], i);    
            switch (pair (playerFace[i]))
            {
                case 1:
                    puts("You have a pair");
                    break;
                case 2:
                    puts("You have two pair");
                    break;
                case 3:
                    puts("You have three of a kind");
                    break;
                case 0:
                    break;
                default:
                    puts("You have four of a kind!");
            }
            if (flush(playerSuit[i]))
                puts("You have a flush!");
            if (straight(playerFace[i]))
                puts("You have a straight!");
            puts("");
        }
        return 0;
    }
    
    /* shuffle the deck */
    void shuffle (int sDeck[][13])
    {
        srand(time(0));
        int row, col;
        for (int cards = 1; cards <= 52; cards++)
        {
            do
            {
                row = rand() % 4;
                col = rand() % 13;
            }
            while (sDeck[row][col] != 0);
            sDeck[row][col] = cards;
        }
    }
    
    /* deal the cards */
    void deal (const int dDeck[][13], const char *dSuit[], const char *dFace[], const int player)
    {
        for (int cards = player * MAXC + 1; cards <= (player+1)*MAXC; cards++)
            for (int row = 0; row <= 3; row++)
                for (int col = 0; col <= 12; col++)
                    if (dDeck[row][col] == cards)
                        printf ("%5s%s%-8s\n", dFace[col], " of ", dSuit[row]);
    }
    
    /* record hands of players into arrays */
    void record (const int dDeck[][13], int hSuit[], int hFace[], const int player)
    {
        for (int cards = player * MAXC + 1; cards <= (player+1)*MAXC; cards++)
            for (int row = 0; row <= 3; row++)
                for (int col = 0; col <= 12; col++)
                    if (dDeck[row][col] == cards)
                    {    
                        hSuit[cards - 1] = row;
                        hFace[cards - 1] = col;
                    }
    }
    
    /* determine if pairs exist in hand */
    int pair ( const int hFace[] )
    {
        int counter = 0;
        for ( int i = 0; i <= (MAXC - 2); i++)
            for ( int j=i+1; j <= (MAXC - 1); j++ )
                if (hFace[i] == hFace[j])
                    counter++;
        return counter;
    }
    
    /* determine if flush exist in hand */
     int flush ( const int hSuit[] )
    {
        for ( int i = 0; i <= (MAXC - 2); i++)
            for ( int j=i+1; j <= (MAXC - 1); j++ )
                if (hSuit[i] != hSuit[j])
                    return 0;
        return 1;
    }
    
    /* determine if straight exist in hand */
     int straight ( const int hFace[] )
    {
        for ( int i = 0; i <= (MAXC - 2); i++)
            for ( int j=i+1; j <= (MAXC - 1); j++ )
                if ((hFace[i] == hFace[j]) || (abs(hFace[i] - hFace[j]) >= MAXC))
                    return 0;
        return 1;
    }

    I asked my friend to run it, and it worked just fine (The cards on hand of player too is determined wrong, but I don't think it is the problem). Can you please have me to identify the error? Maybe because of my compiler?
    I am using Dev-C++ from sourgeforce.net, and I am new to C, so if possible please tell me where I can improve.
    Thank you!
    Last edited by Truong111299; 05-22-2018 at 11:15 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You need to make sure the old process has exited before you try and compile a new version.

    Use task manager or process explorer to watch when your executable gets run, and make sure it disappears when you pass the "press any key to continue".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2018
    Posts
    2
    Quote Originally Posted by Salem View Post
    You need to make sure the old process has exited before you try and compile a new version.

    Use task manager or process explorer to watch when your executable gets run, and make sure it disappears when you pass the "press any key to continue".
    Is this the process you mentioned?

    Strange error when running code-capture-jpg

    I see it appear when I press F10 to run the program, and disappear completely when I pass the "press any key to continue", but it is still not working.

    Ah, I think I have solved it by delete all the executable files in the folder of my code, then recompile the code. It seems to solve the problem, but I still don't know what caused it.
    Last edited by Truong111299; 05-22-2018 at 07:27 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Some virus scanners are known to really mess about with code development.

    The appearance of a new .exe file they've never seen before sends them into a fit. They either keep old exe files 'open' for longer than you expect, they quarantine your attempts at recompiling, or other weirdness.

    It's usually possible to exclude an entire directory tree from being scanned.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-06-2015, 05:15 PM
  2. Code now running but......
    By BenAber in forum C Programming
    Replies: 1
    Last Post: 04-27-2014, 08:42 AM
  3. Very strange error in managed code
    By VirtualAce in forum C# Programming
    Replies: 5
    Last Post: 02-28-2010, 07:49 PM
  4. Running Exe in C++ Code.
    By Ti22 in forum C++ Programming
    Replies: 5
    Last Post: 03-23-2006, 01:29 PM
  5. running code EXACTLY once
    By Trauts in forum C++ Programming
    Replies: 12
    Last Post: 07-11-2003, 12:24 PM

Tags for this Thread