Program should be updating but isn't?

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
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;

void GameLoop();

int main()
{
    char key = ' ';
    HANDLE hConsole;
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    cout << "Welcome to\n";
    SetConsoleTextAttribute(hConsole, 9);
    cout << "SHOOTER\n";
    SetConsoleTextAttribute(hConsole, 7);
    cout << "A C++ game developed by Justin\n";
    cout << "Press any key to begin\n";
    key = _getch();
    if(_kbhit())
    {
        GameLoop();
    }
}

void GameLoop()
{
    int i;
    char field[79][24];
    field[0][12] = 1;   // Player 1's avatar
    field[79][12] = 2;  // Player 2's avatar
    bool bKeepGoing = true;
    int iP1Health = 100;
    int iP2Health = 100;
    char key = ' ';
    while(bKeepGoing)
    {
        i++;
        if(i == 1250)
        {
            for(int j = 0; j < 79; j++)
            {
                cout << endl;
                for(int k = 0; k < 24; k++)
                {
                    cout << field[j][k];
                }
            }
            i = 0;
        }
        while(_kbhit())
        {
            key = _getch();
            cout << "You entered: " << key << endl;
            if(key == 't')
                bKeepGoing = false;
        }
    }
}

The problem with this code is that it should be updating quite often showing the field array, including the 2 ASCII characters. However, once I enter a letter in the main() function, the program simply ends. What am I doing wrong here?

EDIT: Oh some of this code I took from Cazicss's post here: http://www.cplusplus.com/forum/general/29137/
Last edited on
closed account (zwA4jE8b)
you should return 0; in main.

because your if(_kbhit())
only checks that one time which takes the computer a very small fraction of a second to do.

you could do something like

1
2
3
4
5
6
7
8
while(TRUE)
{
 if(_kbhit())
{
  gameloop();
  break;
}
}
So I replaced
1
2
3
4
5
    if(_kbhit())
    {
        GameLoop();
    }
    

with what you suggested, and now it won't let me enter anything, and after a few seconds, the program stops working altogether.
closed account (zwA4jE8b)
sorry, i actually loaded up the code this time you can just write

1
2
    key = _getch();
    GameLoop();


this will wait for _getch() to complete i.e. hitting any key, then run gameloop.

although you are not going to get the desired results from gameloop, but that will solve your first problem.
Last edited on
I assumed key = _getch(); didn't wait for user input (like cin does), so that's why I used kbhit(), so it would only continue once a key had been pressed. I tried your suggestion, and I get the same problem as I did with the previous solution.
closed account (zwA4jE8b)
huh, when I compiled it, it ran.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    char key = ' ';
    HANDLE hConsole;
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    cout << "Welcome to\n";
    SetConsoleTextAttribute(hConsole, 9);
    cout << "SHOOTER\n";
    SetConsoleTextAttribute(hConsole, 7);
    cout << "A C++ game developed by Justin\n";
    cout << "Press any key to begin\n";
    key = _getch();
    GameLoop();
}
Last edited on
Topic archived. No new replies allowed.