Thread: Prevent user input

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    6

    Prevent user input

    Dear forum members,

    my best wishes and a happy newyear to you all!!

    It's the first day of the year and already I have some C problems...lol

    I have to make a game for school. The game shows the player a number and gives the player a few seconds to remember the number. Than the screen is cleared and the number is asked.

    A little piece of code with a problem :

    Code:
                                                                           //A random number between 1 and 4 is being generated                                                                  
    tab1[i] = ((rand()%4)+1);
                                                                           //The player gets to see the number + the time he has to study it
    printf ("New number is = %d\n", tab1[i]);
    printf ("You've got %2d sec to study the number\n", 2+i*2);
    
    
                                                                          //The program is being halted for a few seconds by a do while, afterwards the screen is cleared
    time(&start_time);
    do
    {
    	time(&cur_time);
    }
    while((cur_time - start_time) < 2+i*2);
    
    clrscr();
    
    
                                                                        //The user has to type the same number
    printf ("Please enter the corrent number*\n");
    scanf("%d", &tab2[I]);
    scanf("%*[^\n]%*c");
    When my program is being halted by the do while loop, the user cannot type the number because the screen will not adjust. When he presses a button and hits enter, nothing happens, until the time is passed. But once the time is passed, everything the player typed during the time the program was halted is being accepted by the program. So he can type in the number before the screen is being cleared and cheat! Once the time is over, the program will accept the number the player entered during the halted period.
    It's complicated to explain and my english is poor lol! I hope you guys understand:s I can explain this in more detail if my text doesn't make sense...héhé

    The thing I want to do is: While my program is being halted by the do while, I want to disable user input. Or maybe I can halt the program in another way, so that it comes down to the same point. I'm using borland to program. I cannot use foreign libraries, only the standard ones...

    I tried functions like Msleep(), sleep(), delay(), ... But I can't get them to work with the libraries I have...

    I hope someone can help me Thanks anyway

    Have a nice day

    Tim

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Read FAQ. Here you go which is small document on how to make your thread or the program to wait or sleep for the given amount of time

    How to make my programe to sleep

    ssharish2005

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    6
    I've already read that and it doens't work in Borland :s

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post an example using sleep (of some description), and be more specific than "doesn't work".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Check your compiler to find weather it supports time.h or not. If this header file is supported it should work. And more over most of the compiler will support.

    Well, i don't use borland, so i dunno much about it. Check the compiler documentation for time.h

    ssharish2005

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    6
    Ok, I'll check it out...
    I'll let you guys know within a few moments...and i'll try to be more specific.

    But sleeping my program isn't the biggest concern I think, because
    Code:
    time(&start_time);
    do
    {
    	time(&cur_time);
    }
    while((cur_time - start_time) < 2+i*2);
    works.

    Preventing user input while it is sleeping, that's the problem:s
    Does the sleep() function disable the keyboard when it's sleeping?

    Maybe I have to upload my code so I can make it easier to understand :s

    i'm going to try sleep() now.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I don't think that it is possible to "prevent" userinput during sleep or active waiting as in your example.
    But I think that it is good enough to just ignore any input that has been made during that time.
    So you just have to find a way to discard any input made before prompting for the input.

    I don't know this conio.h functions very well but you could try something like this

    Code:
        while ( kbhit() ) {
            getch();
        }
        clrscr();
        //The user has to type the same number
        printf ("Please enter the corrent number*\n");
        ....
    Kurt

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    well if you use sleep as well, the process or the threads is been suspended for the no of min probably more than a give number of time thats because of the scheduler. When the process is suspended i guess it wont takes any input , i guess. That process wouldn't respond at all. But i am not sure. Need checking that

    ssharish2005

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    6
    I tried using sleep() which is in <unistd.h>. Borland give me an error "could not open unistd.h" LOL

    Next I tried delay() which is in <dos.h>. I got the error "Unidentified function delay in function main()" :S

    Code:
    #include <stdio.h>
    #include <dos.h>
    
    int main(void)
    {
    	char char1;
    
    
    	printf("Please type a character\n");
    
    	delay(10);
    
    
    	scanf("%c", &char1);
    
    	printf("The char you typed is: %c", char1);
    
    	return 0;
    }
    Very simple, but it doens't work :S

    Zuk, I like you're idea to discard the user input...I tried that before using a flush " scanf("%*[^\n]%*c"); ", but it didn't realy do what I thought it was gonna do. But you're code work fine so far It discards the input =D I'm going to give it a good test right now and i'll post the results

    Thanks for all the answers so far

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Note <conio.h> is not a standard library.

    ssharish2005

  11. #11
    Registered User
    Join Date
    Jan 2007
    Posts
    6
    I see...

    I'm using borland v4.52...I forgot to mention it before...It's a schoolversion, lol

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well, it might work in your borland, it wont be portable

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  3. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM
  4. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 06:01 AM