Need to give user more time

This is a discussion on Need to give user more time within the C Programming forums, part of the General Programming Boards category; Anyone know how I can stop the '1' (pressed for first choice) being displayed as the first char in the ...

  1. #1
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123

    Need to give user more time

    Anyone know how I can stop the '1' (pressed for first choice) being displayed as the first char in the string asked for by fcn2? I have tried using kbhit to only call fcn2 when nothing is being pressed and using fflush(stdin) to clear anything in the buffer, but nothing works.

    Another recent post here mentions using delays... how would that be done?

    void menu() {
    char choice;
    printf("Enter Choice:\n\n 1 - First\n 2 - Second\n");
    choice = getch();
    switch (choice) {
    case '1':
    fcn2();
    break;
    case '2':
    fcn3();
    break;
    }
    }

    void fcn2() {
    char accept='n';
    clrscr();
    printf("Type something: ");
    fgets(player.name, 80, stdin);
    }

    thanks...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    > choice = getch();
    You type in "1\n" in response to this, and what you have left on the input stream is "\n"

    > fgets(player.name, 80, stdin);
    fgets returns at the first \n (which will also be the first character).


    So you need to get rid of that spare \n,

    Replace your getch() with this...
    > choice = getch();
    char buff[20];
    fgets( buff, sizeof(buff), stdin );
    choice = buff[0];

    > and using fflush(stdin)
    Because it does nothing - its an undefined operation. Some misguided compilers actually flush the input just to puzzle newbies.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Unregistered
    Guest
    [B]> choice = getch();
    >You type in "1\n" in response to this, and what you have left on the input stream is "\n"<

    Isn't that getchar? Pressing '1' with the code I have jumps straight to the second function... just too quickly, meaning the user is still pressing '1' when asked to input a string.

    If the buffer contained '\n' I wouldn't even be able to write anything would I? player.name would just contain the newline...


    > and using fflush(stdin)
    Because it does nothing - its an undefined operation. Some misguided compilers actually flush the input just to puzzle newbies. <

    Really? Seems to be a common answer here to related questions... oh well... like I said, it didn't do anything for me so I removed it from the code... thought I'd mention I'd tried it tho...

    Cheers...

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Pressing '1' with the code I have jumps straight to the second function... just too quickly, meaning the user is still pressing '1' when asked to input a string.
    You could try and use a pause function before getting the user input in the second function. But as these functions are compiler specific I'm not sure which one you'll need.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    If you're needing a pause funtion here's a simple one that does so for any specified number of whole seconds:
    Code:
    int poz(int x)
     {
     long T1,T2,T3;
     T1=time(&T2);
     T3=(time(&T2)) + x;
     while(time(&T2) < T3);
     return(x);
    }
    poz(3); /// Pause for 3 sec...

    ...be sure to include <time.h>
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    > Isn't that getchar? Pressing '1' with the code I have jumps straight to the second function... just too quickly, meaning the user is still pressing '1' when asked to input a string.
    getch() is unbuffered input and fgets is buffered input.
    There is no reason to assume that these two input streams are synchronised - they appear to be in DJGPP, but for your compiler, it looks as if they are not.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void foo ( ) {
        char buff[10];
        printf( "Enter name > " );
        fflush( stdout );
        fgets( buff, sizeof(buff), stdin );
        printf( "Name=%s", buff );
    }
    int main ( ) {
        int ch;
        printf( "Press '1' > " );
        fflush( stdout );
        ch = getch();
        if ( ch == '1' ) foo();
        return 0;
    }
    
    I can press the keys "1fred\n", and get this output
    Press '1' > Enter name > fred
    Name=fred
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123
    Cheers everyone, I think I'll try all the above suggestions... it only seems a small problem but I'm sure if I ever start writing large programs, it'll be the small bugs that'll get to me the most....

    P.S. Salem:

    I'm using gcc as it came on the cd I got when I started my course. Since then I've found a couple of other compilers but am too used to gcc. I'm all too ready to switch if there is a markedly better one tho...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is the best way to record a process execution time?
    By hanash in forum Linux Programming
    Replies: 7
    Last Post: 03-15-2006, 07:17 AM
  2. Idle time of the user
    By cornholio in forum Linux Programming
    Replies: 6
    Last Post: 12-02-2005, 12:51 AM
  3. The space time continueimnms mm... (rant)
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 06-27-2004, 02:21 PM
  4. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 07:51 PM
  5. give user time to read messages
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 04-21-2002, 01:54 AM

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