1,105,594 Community Members

Program Runs Differently Between Compilers

Member Avatar
MasterMic
Newbie Poster
9 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

First I'd like to say how much I appreciate these forums. They have helped me before, but this is the first time I've posted a question myself.

My problem is that I use a different IDE than my teacher does (probably not the best idea) so I want the program to compile and run properly in his IDE when he tests it.

I use CodeBlocks (although I want to use NetBeans but it's too difficult to set up) and he uses Dev-C++.

So I finished my project in CodeBlocks yesterday and it was working fine. I opened it up in Dev-C++ to test it. I compiled it with Dev-C++ and it compiles, but the program runs differently (incorrectly). The program is a game sort of like tic-tac-toe. Each player takes a turn at placing their "stones". When run from CodeBlocks the game is won when there are 5 of the same "stones" in a row. When run from Dev-C++ the game is won after the first turn, no matter what.

I've looked at my code and can't figure out the problem (since it works fine in CodeBlocks!) so I was hoping someone with more experience could help me understand what is going on.

Thanks. The program is below.

#include <iostream>
#include <iomanip>
#include <stdlib.h>

using namespace std;

int n, type, tries;

int getPadding(int n)
{
    int max(n*n), padding(0);

    while(max > 0)
    {
        padding++;
        max /= 10;
    }
    return (padding+1);
}

bool displayBoard(int board[ ][50], int n)
{
    bool error = 0;
    int padding = getPadding(n);

    cout << endl;

    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            switch (board[i][j])
            {
            case 0:
                cout << setw(padding) << n*i+j+1;
                break;
            case 1:
                cout << setw(padding) << "B";
                break;
            case 2:
                cout << setw(padding) << "W";
                break;
            default:
                error = 1;
            }
        }
        cout << endl;
    }
    cout << endl;
    for(int k=(n*getPadding(n)); k>0; k--)
    {
        cout << "-";
    }

    return error;
}

bool checkBoard(int board[ ][50], int n, int type)
{
    int counterJ(0), counterI(0), counterD(0);

    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            if(board[i][j] == type)
            {
                counterJ++;
            }
            else
            {
                counterJ = 0;
            }

            if(counterJ == 5)
            {
                return 1;
            }
        }
    }

    for(int j=0; j<n; j++)
    {
        for(int i=0; i<n; i++)
        {
            if(board[i][j] == type)
            {
                counterI++;
            }
            else
            {
                counterI = 0;
            }

            if(counterI == 5)
            {
                return 1;
            }
        }
    }


}

int inputN()
{
    int input;

    cout << "Please input a value for n: ";
    cin >> input;
    while (cin.fail() || input < 10 || input > 50)      // does not work for inputs such as 15sdak etc.
    {
        cin.clear();
        cin.ignore(1000, '\n');
        cout << "Sorry, n must be an integer between 10 and 50.\nPlease input an"
             << " acceptable value: ";
        cin >> input;
    }

    return input;
}

int inputPlace(int n)
{
    int input;

    cin >> input;
    while (cin.fail() || input < 1 || input > (n*n))      // does not work for inputs such as 15sdak etc.
    {
        tries += 1;
        if(tries >= 10)
        {
            return -1;
        }
        cin.clear();
        cin.ignore(1000, '\n');
        cout << "\nYour choice must be an integer between 1 and "
             << (n*n) << ".\nYou have " << (10-tries) << " tries remaining.\n"
             << "Please input an acceptable value: ";
        cin >> input;
    }

    return input;
}

void updatePlace(int board[ ][50], int place, int input, int type)
{
    bool error(0);
    while (true)
    {
        if(error)
        {
            place = inputPlace(input);
            if(place == -1)
            {
                return;
            }
        }

        int i = (place - 1)/input;
        int j = place - (i * input) - 1;

        if(board[i][j] != 0)
        {
            tries += 1;
            if(tries >= 10)
            {
                return;
            }

            error = 1;

            cout << "\nThis space is occupied!\nYou have " << (10-tries)
                 << " tries remaining.\nPlease insert an acceptable value: ";
        }
        else
        {
            board[i][j] = type;
            return;
        }
    }
}


main()
{
    bool error(0);

    cout << "\n\n\n                        Welcome to the game of GOBANG!"
         << "\n                        ------------------------------\n\n"
         << "       Player 2 places the white stones. "
         << "Player 1 places the black stones.\n\n"
         << "                To start, you must define the size of the board.\n"
         << "\n                The board is a square with the dimensions n * n."
         << "\n\n\n";

    while (true)
    {
        int type(1), input(0), place;
        int arg[50][50]= {0};

        input = inputN();

        do
        {
            switch (type)
            {
            case 1:
                type = 2;
                break;
            case 2:
                type = 1;
                break;
            default:
                error = 1;
            }

            system("CLS");
            cout << "Player " << type << "'s turn:\n\n";

            displayBoard(arg, input);

            cout << "\n\nWhere would you like to place your stone?: ";
            place = inputPlace(input);
            if(place != -1)
            {
                updatePlace(arg, place, input, type);
            }
            tries = 0;

        }
        while(checkBoard(arg, input, type) == 0);

        system("CLS");
        cout << "The winning board!:\n\n";
        displayBoard(arg, input);

        cout << "\n\nCongradulations! Player " << type << " won!\n\n"
             << "If you would like to play again, enter a new value for n.\n";
    }
}
Question Self-Answered as of 3 Years Ago
Member Avatar
MasterMic
Newbie Poster
9 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I've found the problem! It seems CodeBlocks helps you if you declare a multiple variables with the same name (see: n, type) while Dev-C++ uses the same memory space (I think this is what was happening). I deleted the declarations for type and n at the top and it works now. What a silly mistake.

Also, sorry about this post; I should have condensed the code to the relevant parts and perhaps tried harder to find the error myself.

Thanks! I've attached the updated cpp file in case anyone has a similar problem.

Attachments Untitled1.cpp (5.2KB)
Member Avatar
jonsca
Quantitative Phrenologist
4,273 posts since Sep 2009
Reputation Points: 1,059 [?]
Q&As Helped to Solve: 607 [?]
Skill Endorsements: 14 [?]
Team Colleague
Featured
Sponsor
 
1
 

I'm not certain what the exact problem with your program is, but Dev-C++ is no longer in development, so the one that you can download will almost certainly have an older version of Mingw (Windows port of gcc/g++). That should mean that some portions of your program should run more readily on the Dev-C++ setup if they are less standards compliant. Since you are seeing the opposite effect, I'm not sure.

I did get some warnings when I compiled it with g++ 3.4.2 (which is about a full version behind but not as far back as the one that comes with Dev) and main should return an int according to the standard.

In function `bool checkBoard(int (*)[50], int, int)':
60: warning: unused variable 'counterD'
103: warning: control reaches end of non-void function
 At global scope:
186: error: ISO C++ forbids declaration of `main' with no type
Member Avatar
jonsca
Quantitative Phrenologist
4,273 posts since Sep 2009
Reputation Points: 1,059 [?]
Q&As Helped to Solve: 607 [?]
Skill Endorsements: 14 [?]
Team Colleague
Featured
Sponsor
 
0
 

Glad you found the problem. However, local variables with the same name should "hide" the global variables, AFAIK.

Member Avatar
MasterMic
Newbie Poster
9 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks jonsca; It's useful to know Dev-C++'s no longer in development. My entire school uses it!

The error you received in line 2 is for a part in my code that is unfinished. A bonus for the program was to have the player win if they had 5 in a row diagonally. I just haven't finished it yet. I'm unsure about the other errors.

I'll change the return 1 to return true (standards are good!).

Thanks for taking the time to reply.

Member Avatar
jonsca
Quantitative Phrenologist
4,273 posts since Sep 2009
Reputation Points: 1,059 [?]
Q&As Helped to Solve: 607 [?]
Skill Endorsements: 14 [?]
Team Colleague
Featured
Sponsor
 
1
 

No problem, it was more the second warning that was important. Sorry I didn't specify that. It's saying that there's a path through the if/elses in that function that would result in not returning anything by the end of the function, which has promised to return something.

Member Avatar
MasterMic
Newbie Poster
9 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Oh I see now; I guess my solution was a fluke. I added the "return false" to the end of the 'checkBoard' function and now it's working fine. My slightly educated guess would be that in CodeBlocks the default would be to return a '0' or 'false' but in Dev-C++ perhaps it is returning some random piece of memory. I've added the *actual* fixed function below. Thanks for the help jonsca.

bool checkBoard(int board[ ][50], int n, int type)
{
    int counterJ(0), counterI(0);

    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            if(board[i][j] == type)
            {
                counterJ++;
            }
            else
            {
                counterJ = 0;
            }

            if(counterJ == 5)
            {
                return true;
            }
        }
        counterJ = 0;
    }

    for(int j=0; j<n; j++)
    {
        for(int i=0; i<n; i++)
        {
            if(board[i][j] == type)
            {
                counterI++;
            }
            else
            {
                counterI = 0;
            }

            if(counterI == 5)
            {
                return true;
            }
        }
        counterI = 0;
    }
    return false;
}
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: