Limited asterisked input

This is a discussion on Limited asterisked input within the C++ Programming forums, part of the General Programming Boards category; I've been trying to write the same thing for hours, every different way I could think of, but none work. ...

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Limited asterisked input

    I've been trying to write the same thing for hours, every different way I could think of, but none work. I want to enable the user to type in a password, which on screen will be asterisked, and stop the loop when he hits enter. I tried this :

    Code:
    char pass[30];
    
    int main()
    {
    cout << "Password ?" << endl;
    for(int x = 0; x < 10; x++)
    {
    pass[x] = getch();
    cout << "*";
    if(pass[x] == '\n')
    {
    pass[x] = '\0';
    x = 11;
    }
    }
    
    clrscr();
    cout << endl << endl << pass;
    cin.get();
    
    return 0;
    }
    But this keeps going until I've entered 10 chars. Why doesn't the line "x = 11" break the loop next time around ?


    I also tried something like this :
    [code]
    char pass[30];

    int x = 0;

    void getpass()
    {
    pass[x] = getch();
    switch(pass[x])
    {
    case '\n':
    cout << "*";
    pass[x] = '\0';
    break;
    default:
    cout << "*";
    x += 1;
    getpass();
    break;
    }


    This continues on for ever. Any ideas ?

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

  3. #3
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Can you explain why we need EOF and \r ? How come it doesn't work simply with \n, and what's the meaning of EOF ? I know it's End of File, but we don't have a file here, how is it used ?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    In that example, EOF is used to detect end of input, either through "end of file" or a read error.

    \r is there as an overkill. When reading a text stream, all MS based systems sending \r\n to the app will have these two bytes converted to \n only. Of course, reading from a binary stream will enable your program to receive \r\n.

    >>How come it doesn't work simply with \n,
    As above, on text streams it would be just \n.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Heh... I'm gonna have to ask again, I should have flashed my newbie badge :P Can you explain just a little bit clearer, thanks

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    12
    Code:
    This works but does not handle backspaces which you can work out for yourself.
    #include <iostream.h>
    #include<conio.h>
    
    char pass[30];
    
    int main()
    	{ 
         	int num=1;
    
    		cout << "Enter password " << endl;
    			for(int x = 0; x <num; x++)
    				{
    					num++; 
    					pass[x] = getch();
    					cout << "*";
    
    						if(pass[x] == '\15')
    							{
    
    								pass[x] = '\0';
    								num=num-2;
    								x = num-1;
    							 }
    				}
    
    		clrscr();
    		cout << endl << endl<<"The password you entered was " << pass;
    		cin.get();
    
    		return 0;
    }
    Mod edit:
    it's /code, not \code
    Last edited by bigwullie; 07-10-2003 at 04:20 PM.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Korhedron
    Heh... I'm gonna have to ask again, I should have flashed my newbie badge :P Can you explain just a little bit clearer, thanks
    Which bit are you having trouble with?

    EOF is returned when:
    The end of the input is reached. This is normally signalled by the user hitting CTRL+Z or CTRL+D.
    It can also be returned if the function doing the reading encounters an IO error.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351


    As for the \r and \n:
    \r = carriage return
    \n = newline (or line feed)
    Hence they are also known as CR LF
    Now, on Unix systems, the end of line marker is simply a LF. This is what is generated when you hit [enter]. However, on a Windows based machine, hitting [enter] will result in CRLF being generated. In order to cater for this difference, C/C++ will convert *all incoming LF's and CRLF's to LFs**. This way, your program can check for \n, and is therefore portable, meaning your code can be compiled on both Windows and Unix, and will work correctly.


    * - This is compiler dependant, meaning a Unix compiler won't translate CRs, only LFs.

    ** - This conversion will only be performed on streams opened in text mode, not those opened in binary mode.


    @bigwullie: Your code is a clear case for a buffer overflow exploitation
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Hammer :
    If EOF is returned at the end of input, using getch(), shouldn't the end of input be after the first character, as it is in a simple char a = getch() ?

    Also, the \r \n : If both Unix and Windows use \n, it should detect it under both of these OSs and therefore I shouldn't need to include \r, as the loop breaks when there is a \n...

    Thanks for your previous answer though, and hopefully the next one

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>If EOF is returned at the end of input, using getch(), shouldn't the end of input be after the first character, as it is in a simple char a = getch() ?<<<
    End of the input doesn't mean after the read function (getch in this case) stops reading. EOF is when the OS specifically tells the app that EOF has occured (for whatever reason).

    >>Also, the \r \n : If both Unix and Windows use \n, it should detect it under both of these OSs and therefore I shouldn't need to include \r, as the loop breaks when there is a \n...
    Correct, in my example code \r is probably unnecessary. But then we are dealing with non standard functions, so I can't guarantee it will be the same on all compilers.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    ___
    Join Date
    Jun 2003
    Posts
    806
    Originally posted by bigwullie
    Code:
    This works but does not handle backspaces which you can work out for yourself.
    #include <iostream.h>
    #include<conio.h>
    
    char pass[30];
    
    int main()
    	{ 
         	int num=1;
    
    		cout << "Enter password " << endl;
    			for(int x = 0; x <num; x++)
    				{
    					num++; 
    					pass[x] = getch();
    					cout << "*";
    
    						if(pass[x] == '\15')
    							{
    
    								pass[x] = '\0';
    								num=num-2;
    								x = num-1;
    							 }
    				}
    
    		clrscr();
    		cout << endl << endl<<"The password you entered was " << pass;
    		cin.get();
    
    		return 0;
    }
    Mod edit:
    it's /code, not \code

    I got this error when I tried that out

    Code:
    c:\compiled\password\cpp1.cpp(26) : error C2065: 'clrscr' : undeclared identifier
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>'clrscr' : undeclared identifier
    A non-standard clear screen function. Just remove it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    When does the OS specifically return EOF ? Also, the loop is told to break when there is a new line ( when Enter is pressed ), therefore why doesn't it break without having EOF in the loop ?

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>When does the OS specifically return EOF ?
    When the user hits CTRL+D or CTRL+Z (system dependant). Or when an IO error occurs. Or when the end of the file being input is reached, for example when a file is directed into a program like so:

    c:\>a.exe <input.txt

    >>Also, the loop is told to break when there is a new line ( when Enter is pressed ), therefore why doesn't it break without having EOF in the loop ?<<
    The "while" statement is controlling the loop. All conditions have to be true to enable the code within the loop to be executed.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    >>When does the OS specifically return EOF ?
    When the user hits CTRL+D or CTRL+Z (system dependant). Or when an IO error occurs. Or when the end of the file being input is reached, for example when a file is directed into a program like so:
    Then why would it return EOF here, in which case ? There is no CTRL+D, CTRL+Z, no IO error and no file, therefore no end of file.


    The "while" statement is controlling the loop. All conditions have to be true to enable the code within the loop to be executed.
    Yes, all statements must be true for the loop to execute ( seeing as we are using the && operator ). The loop being :
    Code:
    while ((ch = getch()) != EOF 
              && ch != '\n' 
              && ch != '\r'
    As soon as "ch == '\n'" the loop should break, why doesn't it ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM
  2. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 12:22 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  5. Limited input
    By evilcartman in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2002, 03:29 AM

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