Allowing 1 input only

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • harz
    New Member
    • Mar 2010
    • 2

    Allowing 1 input only

    Hi, I just want to know if is there any way in C to get only 1 input from user?

    Like,
    Enter a letter: a

    After inputting a, should be that I can't anymore type with other letters, only 1.
    I'm thinking that there's something in scanf that needs to be modified a little bit.

    Thanks.

    char letter;

    printf("input a letter: ");
    scanf("%c", &letter);
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    When you say you allow only 1 input, do you mean

    - ignore the rest of the output?
    - throw an error if more output comes in?
    - after touching a single key, the user's keyboard explodes?

    Comment

    • anurag275125
      New Member
      • Aug 2009
      • 79

      #3
      You should use cscanf to allow only 1 input.

      cscanf("%c",&ch ar);

      Comment

      • harz
        New Member
        • Mar 2010
        • 2

        #4
        Thank you for your replies,

        @anurag,
        Thank you for that, just a random question, any other way where in I still need to press enter to verify my input?

        Comment

        • anurag275125
          New Member
          • Aug 2009
          • 79

          #5
          I think there is no other way.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            The answer is: Your program is not running while the uses pounds on the keys. Heaven knows what's been entered. Your program does not regain control until the user presses the enter key-- whenver that is..maybe tomorrow.

            Of course, you could write your own keyboard driver and not use the one provided by the C language.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              You should get in the habit of using this sequence to prompt for keyboard input:
              Code:
              flush stdin
              write prompt string to stdout
              read stdin
              That way you can be sure that you are parsing a response that was entered after the prompt rather than something that has been sitting in the input buffer for a few days.

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                I think it is important to point out that you can't actually call the flush routine (fflush) on stdin for example

                fflush(stdin); /* Bad */

                The standa5rd specifically defines flushing stdin as undefined behaviour.

                However (and I am sure Don meant this) you can flush stdin in so far as you can read and discard all the waiting characters before prompting the user for input using normal stdin read methods such as getchar().

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Thanks, I didn't know that about fflush(stdin).

                  If stdin is buffered, then reading til nothing comes out won't actually flush the stream ... any characters entered after the last newline will still be in there.

                  What's the best way to take care of that? Before I make any other potentially undefined suggestions I did a little research. It says here there is no portable way to empty stdin before printing your annunciator. Ouch!

                  Any ideas?

                  Comment

                  • johny10151981
                    Top Contributor
                    • Jan 2010
                    • 1059

                    #10
                    what about getch and getchar function. have you tried?

                    My bad Banfa already answered it.
                    Last edited by johny10151981; Mar 4 '10, 03:16 PM. Reason: Repeating same answer

                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #11
                      Only ouput streams can by flushed.

                      Input streams are not under your control so you can't flush them.

                      All you can so on stdin is to get characaters until there are no more to get. That means you can't use the scanf family of functions. You will to get a byte at a time and figure out what that byte means in your program.

                      This is not an easy deal. You will end up writing your own keyboard driver.

                      Comment

                      Working...