Help with pause statement.

My program i'snt taking the statement after the pause function properly.

in my program when i pause the program at the begining of the program to prevent the character from moving rapidly to the other side of the screen, it works normally to every command, but the one that directly follows it, please help.

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
#include <iostream>
#include <windows.h>
#include "clearscreen.h"
using namespace std;
char map[14][21] = {
//The x's there are just for display.
//The @ is you.
"+------------------+",
"|                  |",
"|                  |",
"|  @               |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"|                  |",
"+------------------+"};
int main() 
{
        bool exit = false;
        int x=3, y=3, MAP,pause;
        for(MAP=0;MAP<14;MAP++)
        {
        cout << map[MAP] << endl;
        }
    while (exit == false) 
    {
        for(pause=0;pause<100000;pause++)
            if(GetAsyncKeyState(VK_UP)!=0)
            {
                if(map[y-1][x]!= '-')
                {
                map[y][x]=' ';
                y -= 1;
                map[y][x]='@';
                ClearScreen();
                    for(MAP=0;MAP<14;MAP++)
                    {
                    cout << map[MAP] << endl;
                    }
                }
            }
            if(GetAsyncKeyState(VK_DOWN)!=0)
            {
                if(map[y+1][x]!= '-')
                {
                map[y][x]=' ';
                y += 1;
                map[y][x] = '@';
                ClearScreen();
                    for(MAP=0;MAP<14;MAP++)
                    {
                    cout << map[MAP] << endl;
                    }
                }
            }
            if(GetAsyncKeyState(VK_LEFT)!=0)
            {
                if(map[y][x-1]!= '|')
                {
                map[y][x]=' ';
                x -= 1;
                map[y][x] = '@';
                ClearScreen();
                    for(MAP=0;MAP<14;MAP++)
                    {
                    cout << map[MAP] << endl;
                    }
                }
            }
            if(GetAsyncKeyState(VK_RIGHT)!=0)
            {
                if(map[y][x+1]!= '|')
                {
                map[y][x]=' ';
                x += 1;
                map[y][x] = '@';
                ClearScreen();
                    for(MAP=0;MAP<14;MAP++)
                    {
                    cout << map[MAP] << endl;
                    }
                }
            }     
    }
}


for reference, if needed, i defined my own clearscreen function wthout using system functions
What pause function? I don't see any function called "pause" in there.

Your big for loop concerns me. The way you've indented your code, it looks as though you intend all 4 of those if statements to execute within the loop. However, as written, only the first one will. You should enclose in braces all the code that you want to run inside the loop. If you don't, only the first statement will be considered to be inside the loop.

And, frankly, even if you only want to run one statement inside the loop, you should enclose it in braces. It makes the flow of logic easier to understand for you and for anyone else looking at the code, and will help avoid introducing bugs when you come back to modify the code.
Last edited on
What pause function? I don't see any function called "pause" in there.


for(pause=0;pause<100000;pause++)line 32

However, as written, only the first one will.


that is the problem im trying to solve, the first one acts abnormally (moves all the way accross the map, while the last 3 dont and only move 1 space) and i want it to act like my last 3 functions, (so that it moves only 1 space)





If you don't want the for loop to contain anything you have to put empty brackets after it or a semicolon.

for(pause=0;pause<100000;pause++){}

for(pause=0;pause<100000;pause++);

Problem with this is that it might take different amount of time on different computers and the compiler could even optimize away the whole loop because it doesn't do anything. If you want the program to wait for a specified amount of time, you can use the Sleep function instead.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298%28v=vs.85%29.aspx
Last edited on
1-
for(pause=0;pause<100000;pause++)

Most likely the compiler will optimize it anyway. So instead of processing 100000 commands it only performs a single command.

A computer can handle hundreds of million commands (depended of speed of each different computer), that means your 100000... I think it only takes less than 0.1 second...

And the solution? Let's think about a proper function. Why don't you use a function? That is a good knowledge, because you should know the function "Sleep()".

Sleep(milliseconds) - Sleeps the program for "milliseconds
E.g : Sleep(2578) //The program will sleep for 2.578 seconds

Suppose you want to keep the input screen for 5 seconds... The right code is "Sleep(5000);. But you also want to handle keys... That's an another problem.

A method :
- Create a "Sleep" time variable.
- Then, determine the update speed. You can set Sleep(10) //Sleeps for 0.01 second - 100 frames per second
- Then subtract the time variable. (Eg In this case subtract 10 milliseconds..)
- If the time variable is lower than 0 (Time out), break the loop...

You also want "If any proper key is pressed, the program will reset the time interval"? That is easy, reset the time interval.


And the code example :
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
#define TIME_INTERVAL 4000 //4 seconds
int nMilliseconds = TIME_INTERVAL;

..........................
       while (nMilliseconds > 0)
{
            if(GetAsyncKeyState(VK_UP)!=0)
            {
              [...]
//Reset example : nMilliseconds = TIME_INTERVAL
              
            }
            if(GetAsyncKeyState(VK_DOWN)!=0)
            {
               [...]
            }
            if(GetAsyncKeyState(VK_LEFT)!=0)
            {
               [...]
            }
            if(GetAsyncKeyState(VK_RIGHT)!=0)
            {
               [...]
            }     
Sleep(15); //Sleeps for 15 milliseconds (0.015 sec)
nMilliseconds = nMilliseconds - 15;
}



2-

ClearScreen();
for(MAP=0;MAP<14;MAP++)
{
cout << map[MAP] << endl;
}


Making a function for this specific code is better.
Last edited on
Topic archived. No new replies allowed.