Thread: Keyboard input without displaying letters.

  1. #1
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69

    Keyboard input without displaying letters.

    Hi, (Sorry for another question so soon after my last!)

    Is it possible to allow a user to type an input without it displaying on the screen as they type the letters.

    This is in a similar way to logging into Linux using a Terminal.

    For Example:

    The user is prompted for a password, the user enters the password followed by a press of the return key. The password is then assigned to a variable. Nothing that they typed is shown on the screen.

    Is it possible to implement such a thing?

    Thanks for any help

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It sure is possible (how else would anyone do it otherwise), but there is no standard way.
    You need to say which OS/Compiler you're using in order for us to answer the question.

  3. #3
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    ok, well there is a slight problem with os/compiler.

    im using win200/MS VC++ 6 at college and Win XP/Dev C++ at home

    is there code than can compile for both?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You should be able to write a console mode program which compiles in both.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    did you try using /b and getch()?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    Quote Originally Posted by major_small
    did you try using /b and getch()?
    getch() doesnt work in the compilers i use.
    What do you mean by /b ?

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by explosive
    getch() doesnt work in the compilers i use.
    ?
    My dev-c++ has conio.h and getch(). The following illustrates getch() and works on my Windows box when compiled with Borland C, Microsoft VC++, and as a file in a dev-c++ project:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(int argc, char *argv[])
    {
      int c;
      printf("Press ctrl-c to quit\n");
      while ((c = getch()) != 3) {
        putchar(c);
        if (c == '\r') {
          putchar('\n');
        }
      }
      return 0;
    }
    (Of course if you don't want to echo the character, you could use, say putchar('*'), to put an asterisk in place of the user input.)

    Regards,

    Dave
    Last edited by Dave Evans; 12-06-2004 at 09:53 AM.

  8. #8
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    Quote Originally Posted by Dave Evans
    My dev-c++ has conio.h and getch(). The following illustrates getch() and works on my Windows box when compiled with Borland C, Microsoft VC++, and as a file in a dev-c++ project:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(int argc, char *argv[])
    {
      int c;
      printf("Press ctrl-c to quit\n");
      while ((c = getch()) != 3) {
        putchar(c);
        if (c == '\r') {
          putchar('\n');
        }
      }
      return 0;
    }
    (Of course if you don't want to echo the character, you could use, say putchar('*'), to put an asterisk in place of the user input.)

    Regards,

    Dave
    Thanks for that, it works great

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    getch() isn't strictly standard, so while it works now, you may want to eventually find another way to do it. also, \b is a backspace character. on any reasonably fast system, outputting a \b (or series thereof) will make it seem like it was never output at all. that doesn't work so well on input though.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11:28 PM
  2. Keyboard input without screen output?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2001, 02:57 AM
  3. FAQ Keyboard Input ? (C++)
    By Malikive in forum FAQ Board
    Replies: 6
    Last Post: 11-07-2001, 09:30 PM
  4. Keyboard input ?
    By Malikive in forum Game Programming
    Replies: 4
    Last Post: 11-06-2001, 11:14 PM