I am trying to allow user to input something while there is time so I took a function sleep() that I found somewhere and changed it for input. Well, it doesn't work.

char sleep(clock_t wait)
{
    char c;
clock_t goal;
goal = wait + clock();
while(goal > clock()) {
cin << c;
}
return c;
}

Basically I want it to allow input in a given ammount of time. What did I do wrong and what do I have to change?

Ok lol, I got it to work but it waits until user presses enter. I want to change it that only a keypress without enter will send the key to me. Is there any function for that anywhere?

MAIN.CPP

#include "include.h"

using namespace std;
void fall(int &x,int& y, int& y1, int speed) {
 char input = sleep(speed);

 if(input == 'a') {
 x = x-1;
 }
  if(input == 'd') {
 x = x+1;
 }

    gotoxy(x,y1-1);
    cout << "                                     ";
    gotoxy(x,y1++);
    cout << " _                                    ";
    gotoxy(x,y++);
    cout << "|_|                                    ";
    }



int main()
{

    printscreen();
    cursor(0);
int undersx,undersy;

    undersx = getx(),undersy = gety();

int y = 1;
int y1 = 0;
int x = 20;
int speed = 800;

fall(x,y,y1,speed);
fix1st (x,y,y1,speed);



int c = 15;
while(c>0) {
 fall(x,y,y1,speed);
 c--;
}









     gotoxy(undersx,undersy);
    return 0;
}

INCLUDE.h

#include <string>
#include <time.h>
#include <iostream>
#include <fstream>
#include <array>
#include <cstdio>
#include <cstdlib>
#include <typeinfo>
#include <windows.h>
#include <fstream>
typedef unsigned char BYTE;   // 8-bit unsigned entity.
typedef unsigned char byte;   // 8-bit unsigned entity.
// #include <conio.h> //no conio!
//other functions
void leftclick();
void clrscr();
void clear_screen(); //bad one
char sleep(clock_t wait);
//other functions


int kcin(int i);
char kcin(char a);
void cursor(bool show);
void gotoxy(int x, int y);
int getx();
int gety();
void printscreen(void);
void fix1st (int &x,int& y, int& y1, int speed);

FUNCTIONS.CPP

#include "include.h"

using namespace std;

int kcin(int i) {
    int variable;
    while(1) {
    if (cin >> variable) {
break;
}
if(cin.fail()) {
cout << "Input Again: ";
cin.clear();
cin.ignore( 1024, '\n' );  //spend a lot of time on this!
}
}
return variable;
}



 
char kcin(char a) {
    char variable;
    while(1) {
    if (cin >> variable) {
break;
}
if(cin.fail()) {
cout << "Input Again: ";
cin.clear();
cin.ignore( 1024, '\n' );  //spend a lot of time on this!
}
}
return variable;
}


char sleep(clock_t wait)
{
    char c;
clock_t goal;
goal = wait + clock();
while(goal > clock()) {
cin >> c;
}
return c;
}


void cursor(bool show) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursoInfo;
CursoInfo.dwSize = 1;         /* The size of caret */
CursoInfo.bVisible = show;   /* Caret is visible? */
SetConsoleCursorInfo(hConsole, &CursoInfo);
}



void gotoxy(int x, int y)
{
    COORD coord;
    coord.X = x; coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}


int getx() {
CONSOLE_SCREEN_BUFFER_INFO csbi;
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
   GetConsoleScreenBufferInfo(h,&csbi);
return csbi.dwCursorPosition.X;
}

int gety() {
CONSOLE_SCREEN_BUFFER_INFO csbi;
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
       GetConsoleScreenBufferInfo(h,&csbi);
    return csbi.dwCursorPosition.Y;
}




void printscreen(void) {
    cout << " _______________________________________________________\n";
    cout << " |                                                     |\n";
    cout << " |                                                     |\n";
    cout << " |                                                     |\n";
    cout << " |                                                     |\n";
    cout << " |                                                     |\n";
    cout << " |                                                     |\n";
    cout << " |                                                     |\n";
    cout << " |                                                     |\n";
    cout << " |                                                     |\n";
    cout << " |                                                     |\n";
    cout << " |                                                     |\n";
    cout << " |                                                     |\n";
    cout << " |                                                     |\n";
    cout << " |                                                     |\n";
    cout << " |                                                     |\n";
    cout << " |                                                     |\n";
    cout << " |_____________________________________________________|\n";
    }

void fix1st (int &x,int& y, int& y1, int speed) {
    gotoxy(0,0);
 cout << " _______________________________________________________\n";

    sleep(speed);
    gotoxy(x,y1++);
    cout << " _ ";
    gotoxy(x,y++);
    cout << "|_|";
    }

As you can see the screen cleanup is horrible *yet*. I worked on this project for only like half an hour. I just want a cin without the enter and printing what user typed on the screen. And if you find some wierd unncessesery functions it is because I took the functions file from another project. So sorry for that

You can use some non-standard functions found in conio.h, assuming your compiler supports them.

time_t t1 = time(0);
time_t t2 = t1 + 60; // one minute timeout
while( !_kbhit() && t1 < t2)
{
   // do count-down timer
   Sleep(100); // delay 100 milliseconds
   t1 = time(0);
}

if( t2 >= t1)
{
   // timeout expired
}

// not do the cin stuf here

Hey if you want a timer, there is no way u can do it like that especially for input.. that is because cin waits indefinitely and no matter what timer u put, it will keep waiting and then return the time after it has received input..

I believe this would be of relevance to you... NARUE has written this very nicely for me in the past! =) I have learned threading because of it.. and that is the only way to put a timer on cin.. Its to have it run in a separate thread and when the thread timesout, so will cin..

Following Code was written by Narue: :)

#include <windows.h>

namespace jsw {
    namespace threading {
        class auto_event {
        public:
            auto_event(): _event(CreateEvent(0, false, false, 0)) {}

            BOOL wait(DWORD timeout = 1) const
            {
                return WaitForSingleObject(_event, timeout) == WAIT_OBJECT_0;
            }

            BOOL set() { return SetEvent(_event); }
        private:
            HANDLE _event;
        };

        class thread {
        public:
            static thread start(
                LPTHREAD_START_ROUTINE fn, LPVOID args = 0, 
                DWORD state = 0, DWORD timeout = 5000)
            {
                return thread(CreateThread(0, 0, fn, args, state, 0), timeout);
            }

            static void sleep(DWORD milliseconds) { Sleep(milliseconds); }
            static void exit(DWORD exitCode) { ExitThread(exitCode); }
        public:
            thread(HANDLE thread, DWORD timeout): _thread(thread), _timeout(timeout) {}
            ~thread() { CloseHandle(_thread); }

            DWORD exit_code() const
            {
                DWORD exitCode = NO_ERROR;

                GetExitCodeThread(_thread, &exitCode);

                return exitCode;
            }

            HANDLE handle() const { return _thread; }
            BOOL is_alive() const { return exit_code() == STILL_ACTIVE; }
            DWORD join() { return WaitForSingleObject(_thread, _timeout); }
            DWORD suspend() { return SuspendThread(_thread); }
            DWORD resume() { return ResumeThread(_thread); }
            BOOL abort(DWORD exitCode) { return TerminateThread(_thread, exitCode); }
        private:
            HANDLE _thread;
            DWORD _timeout;
        };
    }
}

#include <iostream>
#include <string>

DWORD WINAPI get_password(LPVOID args)
{
    using namespace jsw::threading;

    std::string *s = (std::string*)((LPVOID*)args)[0];
    auto_event *e = (auto_event*)((LPVOID*)args)[1];

    getline(std::cin, *s);
    e->set();

    return NO_ERROR;
}

int main()
{
    using namespace jsw::threading;

    std::string password;
    auto_event e;
    LPVOID args[2] = {&password, &e};

    thread worker = thread::start(get_password, args);

    if (e.wait(5000))
        std::cout<<'\''<< password <<"' was correct\n";
    else {
        worker.abort(NO_ERROR);
        std::cout<<"Invalid password\n";
    }
}

Of course you do have to implement it your way.. in my case, I had to do a password.. and then I had to mask the password aswell.

Anyway that should be more than enough help.

Thanks I will try them both and see which one works better with my code :). I wanted to make a more complex game but I don't really have time for it because I am working on another project and I am doing this one just for fun. Maybe later I will return to this game and make it much better and maybe even add some colors. :)
Anyway thanks for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.