Public Group

This topic is 4885 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I need these things using C++ in dos. A way to record input from the keyboard and a function that waits a certain length of time before continuing. I also want to know how to use rand() so that I can get either 1, 2 or 3 randomly. I've looked briefly but didn't find what I was after so I thought I would ask you guys. :) [Edited by - MikeWulf on June 8, 2005 8:51:17 AM]

##### Share on other sites
[keyboard capture] - sorry, dunno

[function to wait x time] - check the Sleep(ms) function

[howto rand()] - ((rand() % 3) + 1)

hope i helped somewhat.

##### Share on other sites
Also remember that you need to seed rand with a number otherwise you wll always get the same sequence everytime you run your program. Although you should probably not seed rand during most of development since knowing the random number sequence can make debugging alot easier.

Then again if you don't seed rand every so often you can't tell if your program is workng too well, so use your common sense when to switch between seeding and not during development.

Anyway to seed rand just pass any number to the function srand. To get a pretty random number most people seed it with the number from the computer clock like so:

#include <time.h>

srand((unsigned)time(0));

##### Share on other sites
Sweet. Very helpful. All I need now is the keystroke capture and I am set. Anyone? :P

##### Share on other sites
You could try DXInput, although I don't know if it will work in a dos environment.

##### Share on other sites
I'm having trouble finding info on the sleep(ms) function. Could you give me more on it?

##### Share on other sites
Check MSDN and find:

Platform SDK: DLLs, Processes, and Threads
Sleep

The Sleep function suspends the execution of the current thread for at least the specified interval.

To enter an alertable wait state, use the SleepEx function.

void Sleep(
DWORD dwMilliseconds
);

Parameters
dwMilliseconds
[in] Minimum time interval for which execution is to be suspended, in milliseconds.
A value of zero causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run. If there are no other threads of equal priority ready to run, the function returns immediately, and the thread continues execution.

A value of INFINITE indicates that the suspension should not time out.

Return Values
This function does not return a value.

Remarks
This function causes a thread to relinquish the remainder of its time slice and become unrunnable for at least the specified number of milliseconds, after which the thread is ready to run. In particular, if you specify zero milliseconds, the thread will relinquish the remainder of its time slice but remain ready. Note that a ready thread is not guaranteed to run immediately. Consequently, the thread may not run until some time after the specified interval elapses. For more information, see Scheduling Priorities.

You have to be careful when using Sleep and code that directly or indirectly creates windows. If a thread creates any windows, it must process messages. Message broadcasts are sent to all windows in the system. If you have a thread that uses Sleep with infinite delay, the system will deadlock. Two examples of code that indirectly creates windows are DDE and COM CoInitialize. Therefore, if you have a thread that creates windows, use MsgWaitForMultipleObjects or MsgWaitForMultipleObjectsEx, rather than Sleep.

Example Code
For an example, see Using Thread Local Storage.

Requirements
Client: Included in Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, and Windows 95.
Server: Included in Windows Server 2003, Windows 2000 Server, and Windows NT Server.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.

##### Share on other sites
Well, I have finished what I was working on with the help of you guys. If you have any interest it was pong. It has so many annoying bits I can't stand to work on it any more. So here it is if you care to compile it.

#include <iostream>#include <time.h>#include <Windows.h>using std::cin;using std::cout;void display(int PA, int PB, int BX, int BY);int direction(int dir, int ballY);int randcor();int main() {    int ballX=8, ballY=3, ballstate=1;    int paddleA=3;    int paddleB=3;    int dir=-1;    do {        // Sleep(10);        // Ball going right        while(ballstate == 1) {            if(ballX == 14) {                ballstate = 2;                dir = randcor();            }            else {                ballX++;                ballY = ballY + dir;                dir = direction(dir, ballY);                paddleA = paddleB = ballY;                display(paddleA, paddleB, ballX, ballY);            }        }        // Ball going left        while(ballstate == 2) {            if(ballX == 2) {                ballstate = 1;                dir = randcor();            }            else {                ballX--;                ballY = ballY + dir;                dir = direction(dir, ballY);                paddleA = paddleB = ballY;                display(paddleA, paddleB, ballX, ballY);            }        }    } while(ballstate);    return 0;}void display(int PA, int PB, int BX, int BY) {    int i;    Sleep(50);    system("cls");    if((PA<BY) && (PA<PB)) {        for(i=1; i<PA; i++) cout << "\n";        cout << "]";    }    if((BY<PA) && (BY<PB)) {        for(i=0; i<BY; i++) cout << "\n";        for(i=1; i<BX; i++) cout << " ";        cout << "O";    }    if(BY == PA) {        for(i=1; i<BY; i++) cout << "\n";        cout << "]";        for(i=2; i<BX; i++) cout << " ";        cout << "O";        for(i=1; i<15-BX; i++) cout << " ";        cout << "[";    }}int direction(int dir, int ballY) {    if((dir == 1) || (dir == -1)) {        if(ballY == 1) dir = 1;        if(ballY == 6) dir = -1;    }    return dir;}int randcor() {    int dir;    dir = ((rand() % 2) + 1);    switch(dir) {    case 2:        dir = -1;        break;    case 3:        dir = -1;        break;    }    return dir;}

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632996
• Total Posts
3009778
• ### Who's Online (See full list)

There are no registered users currently online

×