If nothing is entered when asked...

This is a discussion on If nothing is entered when asked... within the C Programming forums, part of the General Programming Boards category; I was writing a small program for my sister in 2nd grade. The program itself is quite silly but it's ...

  1. #1
    Registered User Utopus's Avatar
    Join Date
    Jun 2007
    Location
    beautiful place
    Posts
    26

    If nothing is entered when asked...

    I was writing a small program for my sister in 2nd grade. The program itself is quite silly but it's only for my sister, so well, it is silly.
    I wanna ask her to enter "1" if see loves me, and "0" if not. And what I like to add to the function is that if see enter nothing in thirteen given seconds, I like the choice to be automatically "1". How would I write this...?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    void func(void) {
    	printf(".....3\a\n");
    	Sleep(1000);
    	printf(".....2\a\n");
    	Sleep(1000);
    	printf(".....1\a\n");
    	Sleep(1000);
    	system("cls");
    	printf("I love you, Radu!!!\n\a");
    
    	Sleep(2000);
    
    	for(;;) {
    		printf("So much!\a");
    	}
    }
    
    main() {
    
        int choice;
    
        printf("Maria, do you love your brother...?\n");
        printf("If you love him, push \"1\". If you don't, push \"0\". \n>");
        scanf("&#37;d", &choice);
    
        Sleep(10000);
    
    /*I want to automatically input "1" if see does not enter withing Sleep(10000)*/
    /*But how?*/
    	
        func();
    
    	if(choice == '0') {
    		printf("Oh wwwwwwwhhhhhhhhhyyyyyyyyyyy!\n\a\a\a");
    	
    	} else if(choice == '1') {
    		func();
    	}
    
        return 0;
    }
    Last edited by Utopus; 06-22-2007 at 07:00 AM.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You could do something like this for a countdown clock:
    Code:
        unsigned start_time=clock();
        unsigned end_time= start_time+13000;
        while(clock() < end_time)
        {
            //WAIT
        }
    I'm not sure but I thing by using sleep you are making the program delay, therefore the user cant enter anything in the meantime.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    main() {
    
        int choice;
    
        printf("Maria, do you love your brother...?\n");
        printf("If you love him, push \"1\". If you don't, push \"0\". \n>\n");
        choice = getchar();
    
    	if(choice == '0') 
    		printf("Oh wwwwwwwhhhhhhhhhyyyyyyyyyyy!\n\a\a\a");
        else if(choice == '1') 
    		func();
        else if(choice == '\n')
             func();
            
        getchar();    
        return 0;
    }
    But your sister enters either 1 or enter she will have to wait 10000 sec to get theb reply
    ssharish2005

  4. #4
    Registered User Utopus's Avatar
    Join Date
    Jun 2007
    Location
    beautiful place
    Posts
    26
    hmm... it seems that neither of it solve the problem...

    Comrade ssharish, on the if statement's last one, when "choice == \n", this works only if she push Enter. However, what I want is a function that works on this special situation when, suppose, she doesn't even touch the keyboard, and if she doesn't enter anything in required time, I want the program to automatically enter the input of "1".

    As for comrade mike, I quite(?) don't understand how to apply your code...?

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    > As for comrade mike, I quite(?) don't understand how to apply your code...?

    Mike's code won't work any better than Sleep() does. Those kinds of functions still force the program into a pointless, time-wasting execution path, away from the input fetching routine. I believe what you are looking for is something more event driven, but handling no events is usually a non-issue.

    Honestly, you're getting into some system specific stuff, which is more difficult than you intended. If your sister doesn't input anything like 1 or zero, then simply assign 1 to choice like you wanted in that case.

    Just to be clear, I'm not certain why you're rushing things, getchar or scanf will work fast enough on its own if you helped your sister learn the program. You should probably be supervising your sister while she's on the computer anyways, just to make sure she stays safe and doesn't get into anything. It's the best way for her to get the message.

  6. #6
    Registered User Utopus's Avatar
    Join Date
    Jun 2007
    Location
    beautiful place
    Posts
    26
    > If your sister doesn't input anything like 1 or zero, then simply assign 1 to choice like you wanted in that case.

    How would I assign 1 to choice without entering it from keyboard...?

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Well I mean, make choice equal to one.

    choice = 1;

  8. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Hmm maybe what I posted was not very clear. I meant that you could get the input within the while loop. Then if the users enters a 1 or 0 break the loop, otherwise the loop would end after 13 seconds. Hope that makes sense.

  9. #9
    Registered User Utopus's Avatar
    Join Date
    Jun 2007
    Location
    beautiful place
    Posts
    26
    Would you kindly look at my code above and tell me where I have to input "choice = 1"?

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Quote Originally Posted by mike_g View Post
    Hmm maybe what I posted was not very clear. I meant that you could get the input within the while loop. Then if the users enters a 1 or 0 break the loop, otherwise the loop would end after 13 seconds. Hope that makes sense.
    getchar does not work in this way. getchar is line buffered, meaning that it will wait until it can return something, which could disrupt the poster's timing.

  11. #11
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yeah, I just kinda realised that. Is there no method of scanning for a keypress in the console each loop?

    [edit] Something like PollEvent in SDL [/edit]

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You need to use raw keyboard input. Do a search for getch() and you should find some examples. It is pretty compiler/OS specific so we'll need at least those details to help further.

  13. #13
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    In terms of a timer, you could use a separate thread that sleeps for the given amount of time. Then when the time is up.... it forces the selection.

    Also, I'm sick of getch() since it seems too many people use conio.h. I wrote a Windows version of taking input from the console, although I think that was for reading passwords without echoing to the terminal.... Anyway, I would think using an O/S specific form would be better than getch().

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Caution: I'm not a "Windows programmer." But a few minutes on MSDN led me to this:

    Code:
    HANDLE in_handle = GetStdHandle(STD_INPUT_HANDLE);
    DWORD status = WaitForSingleObject(in_handle, 13000);
    if(status == WAIT_OBJECT_0)
    {
        /* She pressed a key. Read the input. */
    }
    else
    {
        /* She didn't press a key within 13 seconds. Set the answer to "1" */
    }
    Perhaps a more Windows-aware programmer can confirm if this might be correct.

  15. #15
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    brewbuck's solution is very nice. The timer expires prematurely, however, if focus is lost on the window. As long as no one switches focus, this should be fine. I imagine a more in-depth one could be written to catch the focus issue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating counters and code not working
    By geekrockergal in forum C Programming
    Replies: 2
    Last Post: 02-04-2009, 11:50 PM
  2. I have some questions :(
    By geekrockergal in forum C Programming
    Replies: 19
    Last Post: 02-01-2009, 08:44 AM
  3. Pass by reference
    By jrice528 in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2007, 01:02 PM
  4. input validation making sure only an int is entered
    By bazzano in forum C Programming
    Replies: 10
    Last Post: 09-06-2005, 06:14 AM
  5. Need HELP with Program
    By Jeff in forum C Programming
    Replies: 25
    Last Post: 09-23-2004, 08:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21