Thread: Help in explaining this short program

  1. #16
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by sturm100
    I see, thanks a lot

    well carraige return means the cursor at the beginning right?

    by the way, how do one know is a ascii code? from the code it looks like a number of char string to me....

    how u see that so fast?
    I see it so fast 'cos I've practiced a lot and am experienced in these things, thats all.

    The ASCII chart is available at the link I gave in my earlier post.

    >while ((pass[num] = getch()) != 13)
    I recognised this because getch() returns the ASCII value of the character it read in. Therefore, it is testing against an ASCII 13 (CR).

    Now, about CR. One a Windows machine, the end of a line is denoted by the ENTER button. But underneath the hood, the OS sees this as a Carriage Return AND Line Feed (2 bytes together). These are 13 and 10, or in hex 0x0d and 0x0a. If you have a hex editor, open any text file, and look for these values to see what I mean.

    On a Unix machine, end of line is denoted by only one byte, 10 (0x0a).

    It is not good practice to hardcode these values in your source though. You should really use these equivalents:
    \n
    \r
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  2. #17
    Registered User
    Join Date
    May 2002
    Posts
    33
    so should the below ( num!=8) or should i change it to (num!=9) if my password string is [9]? is this another ascii code?

    PHP Code:

    if (num != 8)
          {
        
    num 0;
        while ((
    pass[num] = getch()) != 13)
          {
            
    putchar('*');
            
    num++;
          }
        
    pass[num] = '\0';
          }
        else
          goto 
    Loop

  3. #18
    Registered User
    Join Date
    May 2002
    Posts
    33
    Originally posted by Hammer

    I see it so fast 'cos I've practiced a lot and am experienced in these things, thats all.

    The ASCII chart is available at the link I gave in my earlier post.

    >while ((pass[num] = getch()) != 13)
    I recognised this because getch() returns the ASCII value of the character it read in. Therefore, it is testing against an ASCII 13 (CR).

    Now, about CR. One a Windows machine, the end of a line is denoted by the ENTER button. But underneath the hood, the OS sees this as a Carriage Return AND Line Feed (2 bytes together). These are 13 and 10, or in hex 0x0d and 0x0a. If you have a hex editor, open any text file, and look for these values to see what I mean.

    On a Unix machine, end of line is denoted by only one byte, 10 (0x0a).

    It is not good practice to hardcode these values in your source though. You should really use these equivalents:
    \n
    \r
    I think now i finally understand. 13 can be replace by "\n" right?

  4. #19
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {
    	int ch;
      	printf ( "Enter your password: " );
      	while ( ( ch = getch() ) != '\r' )
        	{
        		putchar ( '*' );
        	}
        	
      	printf ( "\nThank you\n" );
      	return 0;
    }
    1. Accept characters.
    > while ( ( ch = getch() ) != '\r' )
    If the enter key is not pressed, keep accepting them.
    2. Display an asterik for each one.
    The world is waiting. I must leave you now.

  5. #20
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by sturm100


    I think now i finally understand. 13 can be replace by "\n" right?
    As Shadow said, \r is 13. \n is 10.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #21
    Registered User
    Join Date
    May 2002
    Posts
    33
    Oh my mistake

    thanks

    but what about the (num!=8) ? is this an ascii ?

    I am now quite clear except the above mention part

    BTW, where are u all from?

  7. #22
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > BTW, where are u all from?
    *sings*
    Somewhereeeee, overrrr the rainboooow....
    The world is waiting. I must leave you now.

  8. #23
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >but what about the (num!=8) ? is this an ascii ?
    No. This is a straight numeric test. However, the coding is incorrect. Notice how num isn't set to any value before it comes to this statement? That is wrong, because num could be any value. I don't think you need the if statement anyway.

    Theres another problem, as well.
    >while ((pass[num] = getch()) != 13)
    This will keeping adding characters to the array as the user enters them. At some point, the array size will be exceeded (pass is only 9 bytes), and you will start writing over memory that is not yours! Bad mistake, I'm afraid! It also doesn't allow for the user trying to delete a character (backspace is just another key entry as far as getch() is concerned).

    Here's some code I posted somewhere else. Have a look through and see if you understand it OK. Don't use it if you don't want to, I only provide it as an example.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <ctype.h>
    #define PASSLEN 10
    
    int main(void)
    {
    	int		ch, i;
    	char	password[PASSLEN + 1];
    	printf("Enter your password: ");
    	i = 0;
    	while ((ch = getch()) != '\r' && i < PASSLEN)
    	{
    		if (ch == '\b')
    		{	/* Handle backspace */
    			if (i > 0)
    			{
    				i--;
    				printf("\b \b");
    			}
    
    			password[i] = '\0';
    		}
    		else if (isalnum(ch))
    		{	/* Only characters and numbers considered to be part of the password */
    			password[i] = ch;
    			putchar('*');
    			i++;
    		}
    	}
    
    	password[i] = '\0';
    	printf("\nThank you, you entered: %s\n", password);
    	return(0);
    }

    >BTW, where are u all from?
    England.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. My Allegro Program will not run...
    By Vicious in forum Game Programming
    Replies: 17
    Last Post: 06-14-2002, 12:49 AM
  4. Trying to make rand different per program run
    By Dreamerv3 in forum C++ Programming
    Replies: 6
    Last Post: 01-18-2002, 03:26 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM