Thread: ENTER in getch()

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    12

    ENTER in getch()

    Given below is a code to enter a password.A $ sign is entered 2 indicate end of password.

    Code:
    printf(" Enter Password  : );
           while( ((pwde[i] = getch()) != '$'  )   )
          {  printf ("*");
           i++;
          }
        pwde[i]='\0';
    But how do i modify this code if i want to press ENTER after password is input. How does getch interpret ENTER ?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Change '$' to '\n'
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Change '$' to '\n'
    i was trying the problem given by hello and changed '$' to '\n' but that doesn't worked.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    12
    I got the solution. Replace '$' with 13. That is the ASCII value of ENTER.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hello
    I got the solution. Replace '$' with 13. That is the ASCII value of ENTER.
    That is a bad solution because it is not descriptive (and not portable, but then getch() is not portable and ASCII is likely). You should use cpjust's solution, or a variant of it where you use '\r' (which is 13 in ASCII).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Damn, I keep mixing up \r & \n
    Why do they even need both of them anyways? I think it must be some throwback to ancient manual typewriters or something.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With a BlackJack Program in C
    By Jp2009 in forum C Programming
    Replies: 15
    Last Post: 03-30-2009, 10:06 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  4. Assignment output help
    By Taka in forum C Programming
    Replies: 13
    Last Post: 09-23-2006, 11:40 PM
  5. hi need help with credit limit program
    By vaio256 in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2003, 12:23 AM