Thread: remaining input from getch()

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    8

    remaining input from getch()

    Hello C board,

    At the end of my C program, I want to ask to the user if he want to start again, simply by pressing y or n. So I wrote this :
    Code:
    void main()
    {
    char a$;
    ...
    ...
    ...
    printf("Start again (y/n)");
    a$=getch();
    if (a$=='y') main();
    }
    OK, this works, but at the first scanf in my program (to ask to the user to enter a number for example), the 'y' is displayed on the screen, so the user have to erase it with backspace before entering the number.
    How can I change this behaviour ?

    All the best

    Pierre

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >void main()
    void main is not correct, use
    Code:
    int main  ( void )
    instead.

    >char a$;
    The $ isn't a standard character for identifiers. It may work for you, but your code is not portable.

    >if (a$=='y') main();
    Don't call main recursively, use a loop instead:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <conio.h>
    
    int main ( void )
    {
      int yn = 'y';
    
      while ( tolower ( yn ) == 'y' ) {
        printf ( "Doing stuff\n" );
        printf ( "Do more stuff? (y/n): " );
        fflush ( stdout );
        yn = getch();
        printf ( "\n" );
      }
    
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    8
    Waow, thanks for the fast answer !!!


    >void main()
    >void main is not correct, use
    >int main ( void )
    >instead.

    I see void main() in a tutorial, and there is no warning about this with the compiler.

    >char a$;
    >The $ isn't a standard character for identifiers. It may work for you, but your code is not >portable.

    Well, in fact, before to try C, I spent many years programming in Basic. Hence the $ for strings. May be I kept bad habits...

    Your code work well but don't solve the problem.
    If I add a scanf in the program :
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <conio.h>
    
    int main ( void )
    {
      int yn = 'y',loc;
    
      while ( tolower ( yn ) == 'y' ) {
        printf ( "Enter a number\n" );
    /* next come the unwanted behaviour*/
        scanf("%d",&loc);
        printf ( "Do more stuff? (y/n): " );
        fflush ( stdout );
        yn = getch();
        printf ( "\n" );
      }
    
      return 0;
    }
    After a y keypress, a 'y' is displayed on the screen when processing the scanf command :

    ****Display****

    Enter a number
    123
    Do more stuff? (y/n):
    Enter a number
    y_

    ***************

    I don't want this display of 'y'. How can I do ?

    All the best

    Pierre

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I see void main() in a tutorial
    Tutorials are often wrong.

    >and there is no warning about this with the compiler.
    Compilers can be wrong and often have extensions that allow common practices.

    >Your code work well but don't solve the problem.
    What compiler do you use? My tests all work fine (on a range of modern/popular ones).
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    8
    Hello 'prelude'

    I use LCC-Win32, and you ?

    I forgot a question about your comments :
    What is wrong for the main procedure to call itself ? ( if (a$=='y') main(); )

    Thanks again for your help

    All the best
    Pierre

  6. #6
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    What is wrong for the main procedure to call itself ? ( if (a$=='y') main(); )
    Recursions always have to have an end. This doesn't know if it will ever end. It will keep copying main onto the stack unto you run out of memory and your program crashes.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >This doesn't know if it will ever end.
    Of course it does, the recursion chain will stop when a$ is not 'y'.

    >I use LCC-Win32, and you ?
    My tests were on Visual C++ .NET, Dev-C++ and Borland 6. Mileage varies on the functionality of getch, so my first reaction would be to say you have a funky implementation.

    >What is wrong for the main procedure to call itself ? ( if (a$=='y') main(); )
    Calling main recursively is legal, but very bad style. The only real use it has is in the IOCCC.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  2. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  3. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  4. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM