Program stops running when it reaches a certain point

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <windows.h>
using namespace std;

// Functions
void GameBegin(int selector);
void NewGame();
void LoadGame();
void CreateGameBoard();
void PlayGame();

// Global Variables
enum { MENU, PLAYING };
char cTable[14][14];

int main()
{
    HANDLE hConsole;
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    cout << "Welcome to";
    SetConsoleTextAttribute(hConsole, 207);
    cout << " ANCIENT WARFARE" << endl;
    SetConsoleTextAttribute(hConsole, 7);
    cout << "A C++ Program developed by Justin B------\n";
    cout << "\n(If you don't know how to play, see the Readme file)\n";
    cout << "This game requires 2+ people to play.\nPlease select an option, then press enter:\n";
    bool bBadEntry = false;
    int iMenuSelect;
    do
    {
        if(bBadEntry == true)
            cout << "\nBad input! Try again:\n";
        cout << "1. New Game\n2. Load Game\n3. Exit\n";
        cin >> iMenuSelect;
        if(iMenuSelect <= 3 && iMenuSelect >= 1)
        {
            bBadEntry = false;
            GameBegin(iMenuSelect);
        }
        else
            bBadEntry = true;
    } while(bBadEntry == true);
}

void GameBegin(int select)
{
    switch(select)
    {
        case 1:
            NewGame();
            break;
        case 2:
            LoadGame();
            break;
        case 3:
            exit(1);
            break;
    }
}

void NewGame()
{
    int iNumPlayers;
    CreateGameBoard();
    do
    {
    cout << "\nHow many players will there be? (Up to four)\n";
    cin >> iNumPlayers;
    if(iNumPlayers > 4 || iNumPlayers < 1)
        cout << "\nInvalid number of players. Please re-enter.\n";
    } while(iNumPlayers > 4 || iNumPlayers < 1);

    switch(iNumPlayers)
    {
        case 4:
            cTable[7][14] = 6;
        case 3:
            cTable[7][0] = 5;
        case 2:
            cTable[14][7] = 4;
        case 1:
            cTable[0][7] = 3;
            break;
    }
}

void LoadGame()
{
}

void CreateGameBoard()
{
    int min = 1;
    int max = 10;
    int iRandomNumber;
    int iRandomNumber2;
    int playOrReshuffle;
    int iHiddenTable[14][14];
    for(int i = 0; i < 15; i++)     // Creates the hidden table, which will serve to map the real table
    {
        iRandomNumber = min + rand() % max;
        for(int j = 0; j < 15; j++)
        {
            iRandomNumber = min + rand() % max;
            iHiddenTable[i][j] = iRandomNumber2 = min + rand() % max;
        }
    }

    for(int i = 0; i < 15; i++)         // Creates the real table, based on the randomized hidden table.
    {                                   // If the hidden table int is 1, it creates water. Otherwise, it
        for(int j = 0; j < 15; j++)     // creates land.
        {
            if(iHiddenTable[i][j] != 1)
                cTable[i][j] = 254;
            else
                cTable[i][j] = 177;
        }
    }
    cTable[0][7] = 5;       // Creates the players' bases. 5 = Club
    cTable[14][7] = 6;      // 6 = Spade
    for(int i = 0; i < 15; i++)
    {
        cout << endl;
        for(int j = 0; j < 15; j++)
        cout << cTable[i][j];
    }
    cout << "\n1. Play this map?\n2. Or reshuffle and make a new map?\n";
    cin >> playOrReshuffle;
    if(playOrReshuffle == 2)
        CreateGameBoard();
}

void PlayGame()
{
    cout << "\nPlayer One, it is your turn";
}

(Note that PlayGame() isn't finished)
When I run this code, and enter the sequence "1" followed by "1" (New Game > Play This Map?), it says "Ancient Warfare.exe has stopped working" and I get the error in the compiler "Process terminated with status -1073741510 (0 minutes, 12 seconds)" (time varies, of course). Does anyone know what I'm doing wrong?
Last edited on
This one was trickey but it appears to be a scope issue when you return from your "CreateGame()" function. Declaring 'iHiddenTable[14][14]' on line 105 to be static should fix the crash.
Last edited on
Thank you so much! It's fixed now. :)
The loops in CreateGameBoard() are going out of bounds, treating cTable and iHiddenTable as arrays of 15x15 elements when they are actually 14x14. It would be alot cleaner if you use constants to define the board size. E.g.,

1
2
3
4
5
6
7
8
#include <iostream>
#define BoardWidth 15
#define BoardHeight 15
...
int iHiddenTable[BoardWidth][BoardHeight];

for (int y = 0; y < BoardHeight; y++)
     for (int x = 0; x < BoardWidth; x++)


Also, cTable should probably be type unsigned char. If you move to a compiler where char is signed by default then you will have truncation problems, particularly with this line:

cTable[i][j] = 254;
Topic archived. No new replies allowed.