3

For a program I'm writing, I wish to read in characters from the keyboard using cin until a certain amount of time has passed. The user can enter as many character as they wish (from 0 to however many possible), and the program needs to store the characters in an array for use later. Ideally, the user should not have to press enter after each letter; the program should be able to read in the input without any line breaks and stop once the specified time is up. I've been looking for a while and have tried different methods, including using getline, usleep, and even SDL, but nothing I've tried has worked. This is my latest attempt:

#include <iostream>     
using namespace std;

#include <time.h>

const float frame_length = 1;

int main () {

    char test[40];

    clock_t t;
    t = clock();
    int counter = 0;

    while (clock() - t < (1000000 * frame_length)){
        cin >> test[counter];
        counter ++ ;
    }

     cout << endl << "STOP" << endl;
}

The problem here is that the loop is not stopping after the specified time at all (when I replaced the cin line with a cout line it did) and the user still needs to press enter every time. Is it at all possible to read in input the way I wish? If it helps, I'm working on a Mac. Any help would be much appreciated.

sslate727
  • 33
  • 6

1 Answers1

1

Since you said deally, the user should not have to press enter after each letter; the program should be able to read in the input without any line breaks, your program will fail to meet above requirements since std::cin blocks the current thread till user hit the ENTER key. what you can do is constantly monitor the key states. if a key has pressed in a given time add that character to your test array. if you are in windows environment, you can use GetAsycKeyState function for this purpose.

Here is a modified code,

#include <iostream>


#include <Windows.h>
#include <time.h>

const float frame_length = 1;

using namespace std;
int main () {

    char test[40];

    clock_t t;
    t = clock();
    int counter = 0;

    while (clock() - t < (3000 * frame_length)){

        //cin >> test[counter];

        //i have added Sleep(200) here to prevent    
        //picking up a single key stroke as many key strokes.
        Sleep(200);

        for(int key = 0x41; key < 0x5a; key++)
        {
            if(GetAsyncKeyState(key))
            {
                test[counter] = char(key);
                cout << char(key) << endl;
                counter ++ ;

                break;
            }

        }




    }


    cout << endl << "STOP" << endl;
}
chathux
  • 821
  • 8
  • 17
  • Thanks so much for your help! I probably should have mentioned this earlier, but unfortunately I'm working on a mac. Is there a version of GerAsyncKeyState for macs? – sslate727 Aug 16 '15 at 03:36
  • may be this will help you http://stackoverflow.com/questions/16150267/mac-os-x-get-state-of-spacebar – chathux Aug 16 '15 at 03:43
  • Is Carbon something I need to download? I don't have it in my frameworks. I'm running OS X. Thanks again for all your help! edit: I also just read that it was deprecated https://en.wikipedia.org/wiki/Carbon_%28API%29 – sslate727 Aug 16 '15 at 04:09