Thread: Help in explaining this short program

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    33

    Help in explaining this short program

    I was given this short program on password and works perfectly. I understand all parts except the below statement.

    PHP Code:
    if (num != 9)
        {
             
    num 0;
        while ((
    pass[num] = getch()) != 13// [I]why 13?[/I] 
          
    {
            
    putchar('*');
            
    num++;
          }
        
    pass[num] = '\0';
          }
        else
          goto 
    Loop
    Please help me to understand the place where I put a //
    Last edited by sturm100; 05-30-2002 at 03:18 AM.

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > goto Loop;
    Blech!

    > // why 13?
    That's c++ commenting BTW.

    Prelude posted a password masking code a little ways back, do a search for it.
    The world is waiting. I must leave you now.

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Actually
    Code:
    /*
    
    	Program:
    	Pwd.c
    	
    	Purpose:
    	Demonstrates replacing characters with ****,
    	in the event of hiding a password.
    	
    */
    
    #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;
    }
    Who uses the search function anymore.

    > // why 13?
    To expand, the password must be preset to 13 characters.
    The above example simply waits untill the "return key" is pressed.
    The world is waiting. I must leave you now.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    33
    I mean i understand the code but why a 13 and not 12 or 14 or 9? I tried putting a 9 and the system hang

  5. #5
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > I mean i understand the code
    So do I. It's poor.
    The world is waiting. I must leave you now.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    33
    i have even enter more than 13 characters and still it wont prompt for wrong password till i press enter.

    By the way, I like this code coz its able to allow the user to key in 3 tries before the program shutdown, and the password is mask by *

  7. #7
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    I like this code coz its able to allow the user to key in 3 tries before the program shutdown, and the password is mask by *
    The code I posted masks input this way too.
    However, you code everything else.
    Unlimited tries until they decide to stop.
    Preferably, without goto, while using correct /* C comments. */

    [EDIT]
    BTW, if you want help for this question, post all code relevant to the password section of the program. Anything that would effect it.
    [/EDIT]
    The world is waiting. I must leave you now.

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    33
    Actually I have no problem running the program, just that I have to explain why the statement is written this way.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    13 is an ASCII carriage return, that's why your original loop was checking for it.

    www.asciitable.com
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    33
    ok heres the program, as i am a beginner, i wanted to learn why its written 13 and 9. and why when i replace the "char pass[9] to [8], the whole program goes to endless loop. if i am not wrong the [9] denotes the number of characters in the password but how come i can only put 8 char instead (if i add one more, the program will go to endless loop when i key in the correct password?

    PHP Code:

    void password
    (void)
      {
        
    char pass[9], password[9] = "pass1234";  /*password will be max at 8 characters*/
        
    int numrepeat 0;

        
    Loop:
        if (
    repeat 2)   /*if password enter incorrect 3 times, prompt and exit*/
          
    {
        
    clrscr();
        
    mainpage();
        
    gotoxy(30,30); printf("Check your password and try again.");
        
    getch();
        exit(
    1);
          }
        
    clrscr();
        
    mainpage();
        
    gotoxy(23,20);printf("Please Enter Password: ");
        if (
    num != 9)
          {
        
    num 0;
        while ((
    pass[num] = getch()) != 13)
          {
            
    putchar('*');
            
    num++;
          }
        
    pass[num] = '\0';
          }
        else
          goto 
    Loop;

        if (
    strcmp(passpassword) == 0/* password comparison
          {
        clrscr();
        mainpage();
        gotoxy(23,20);
        printf("Access granted!");
        getch();
        menu();
          }
        else
          {
        clrscr();
        mainpage();
        gotoxy(23,22);
        printf("Access denied!");
        repeat++;
        getch();
        goto Loop;
          }
      } 

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by sturm100
    i wanted to learn why its written 13 and 9.
    It's 13 because of what I said in my last post.

    Its 9 here
    >password[9] = "pass1234";
    because the length of the string is 8 characters, and you need 1 extra to hold the NULL terminator that denotes the end of the string.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > goto Loop;
    > Loop:
    Loop.
    The world is waiting. I must leave you now.

  13. #13
    Registered User
    Join Date
    May 2002
    Posts
    33
    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 trying to make sense of that part... really difficult to understand...

    Thanks for the pointer

  14. #14
    Registered User
    Join Date
    May 2002
    Posts
    33
    Originally posted by Shadow
    > goto Loop;
    > Loop:
    Loop.
    ?????
    i dun understand.....

  15. #15
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > well carraige return means the cursor at the beginning right?
    ..of the next line, yes.
    The world is waiting. I must leave you now.

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