• Forum
  • Lounge
  • Yay! Found a delay function and clear fu

 
Yay! Found a delay function and clear function!

Observe:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef FAC_H_
#define FAC_H_
#endif
#include <windows.h>
float delay(float millisec){ 
clock_t timesec; 
timesec = clock(); 
while((clock() - timesec) < millisec*1000){}
return millisec*1000; } 
void clear(){
  HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  COORD coord = {0, 0};
  DWORD count;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  GetConsoleScreenBufferInfo(hStdOut, &csbi);
  FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
  SetConsoleCursorPosition(hStdOut, coord);}
 

Now, for implementation:
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
#include <iostream>
#include "fac.h"
using namespace std;

int main(){
 #include <iostream>
#include "fac.h"
using namespace std;

int main(){
    int num;
    char doag;
    while(true){
    clear();
    cout << "How long should the countdown go in seconds? ";
    cin >> num;
    for(int i = num; i > 0; i--){
    cout << i << "! ";
    delay(1);}
    cout << "BOOM! ";
    delay(1);
    cout << "Restart? [y/n] ";
    cin >> doag;
    if(doag == 'n' || doag == 'N')
    break;}
    return 0;
}
}
Last edited on
what was wrong with sleep(ms); ?
Couldn't find the lib.

I also wanted something besides SYSTEM("CLS").
forgive me qwerty, but I'm not understanding how to use or implement the clear and delay functions above. Do I combine the two program blocks as one program? When I tried to compile them, it said "clock_t" was undefined. do I define it as an int or float before it is used, or should it be included in the library??
clock_t is a type in the time library, IIRC.
Whoops.
OK.. so now the compiler says can't find file "fac.h"
do I create a file (called fac.h) and put the text in the upper block in that file???
Yes.
fac.h was the library I stored the functions in.
Got it!!! thanks for passing this on, and helping me implement it. No more using the "dreaded system(cls)"!!!

So... if I understand this correctly, I can create a C++ library file with any complex piece of code that does a specific function; #include<it>, and with a simple command perform all of the "hidden" code in any future program I write??? wow!
Last edited on
Not exactly. To use a header file that is in your normal working directory, replace the arrow brackets with quotes (i.e. #include "it").
If the header file is in the libraries, then use the arrow brackets.
ok, nice to know the distinction.

now I have a new problem, and this ties into another thread I was on;
the following code is designed to check if a keyboard key is pressed. if one is, it evaluates what to do with it, if not, it continues to display outputs. I had this program working, but when I put the delay function in, it interferes with the rest of the program?!?!? is the return millisec*1000; line confusing the program with a value it doesn't know what to do with?? and what can I do with it?
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
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <fac.h>
using namespace std;

char changedir(char *prompt){
     DWORD           mode;
     HANDLE          hstdin;
     INPUT_RECORD    inrec;
     DWORD           count;
     char            result='\0';
     
     hstdin = GetStdHandle( STD_INPUT_HANDLE );
     if (hstdin == INVALID_HANDLE_VALUE
     || !GetConsoleMode( hstdin, &mode )
     || !SetConsoleMode( hstdin, 0))
      return result;
     
    
WriteConsole(
GetStdHandle( STD_OUTPUT_HANDLE ),
prompt,
lstrlen( prompt ),
&count,
NULL
);
FlushConsoleInputBuffer( hstdin );

ReadConsoleInput( hstdin, &inrec, 1, &count );
result = inrec.Event.KeyEvent.uChar.AsciiChar;
return result;
}

bool iskeypressed()
  {
  return WaitForSingleObject(
    GetStdHandle( STD_INPUT_HANDLE ),
    0
    ) == WAIT_OBJECT_0;
  }

int main(){
    int x=0,y=0,z=1;
    bool keypress = false;
    char dir='\0';
    while(z == 1 ){
                
                keypress = iskeypressed();
                if (keypress == true)
                   {
                   dir = changedir(" ");
                   if (dir == 'n') y++;
                   else if (dir == 's') y--;
                   else if (dir == 'e') x++;
                   else if (dir == 'w') x--;
                   else if (dir == 'q') z++;
                   keypress = false;
                   }
                cout << x <<"/" << y;
                delay(0.2);
                cout << '\n';
                }
    return 0;
}
What do you mean you "couldn't find the lib"?

Am I the only one seeing the problem with performing a multiplication, a subtraction, and a float to integer conversion (this is just ridiculous) inside the condition of a tight loop?
I think what you meant to do was this:
1
2
3
4
5
void wait(int seconds){
	clock_t endwait;
	endwait=clock()+seconds*CLOCKS_PER_SEC;
	while (clock()<endwait);
}

CLOCKS_PER_SEC varies from system to system. In Windows it's 1000, in Linux it's 1000000.
In any case, this a horrible way to do this if you don't like to see your CPU running a marathon while your program is taking a nap.
Last edited on
I wanted it to be in seconds.
Wouldn't
1
2
3
4
5
6
7
#include <windows.h>

void wait(int seconds){
    Sleep(seconds*1000);
}
//Or, even better (it's not "wait secks", okay? It's "wait secs"):
#define WAITSECS(x) Sleep((x)*1000) 

have been shorter and... well, better? I mean, if you're already using the WinAPI, you might as well use it in its entirety.
...nyeh.
No, your reason is perfectly valid. Just didn't want to use a shortcut.
...nyeh.
return millisec*1000; }

You should NEVER put the closing-bracket on the same line as a statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef FAC_H_
#define FAC_H_
#endif
#include <windows.h>
float delay(float millisec){ 
clock_t timesec; 
timesec = clock(); 
while((clock() - timesec) < millisec*1000){}
return millisec*1000; } 
void clear(){
  HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  COORD coord = {0, 0};
  DWORD count;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  GetConsoleScreenBufferInfo(hStdOut, &csbi);
  FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
  SetConsoleCursorPosition(hStdOut, coord);}

At first glance, looks like a single function that's actually incomplete. That code is very much unreadable.



Better: (Using auto-format by Eclipse+CDT with K&R Style)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef FAC_H_
#define FAC_H_
#endif

#include <windows.h>

float delay(float millisec) {
  clock_t timesec;
  timesec = clock();
  while ((clock() - timesec) < millisec * 1000) {
  }
  return millisec * 1000;
}

void clear() {
  HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  COORD coord = { 0, 0 };
  DWORD count;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  GetConsoleScreenBufferInfo(hStdOut, &csbi);
  FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y,
      coord, &count);
  SetConsoleCursorPosition(hStdOut, coord);
}
Last edited on
Actually his use of braces looks similar to the Pico style...
http://en.wikipedia.org/wiki/Brace_style#Pico_style
Topic archived. No new replies allowed.