Thread: Password Checks 3 times Problem

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

    Unhappy Password Checks 3 times Problem

    Hey guys I'm a newbie in C programming and I had a hard time trying to figure out whats the problem with my codes:
    Code:
    #include <stdio.h>
    #include <string.h>
    main()
    {
    char pass1[20];
    char pass2[]= "password";
    int i=0;
    	do
    	{
    	i++;
    	clrscr();
    	printf("Enter Password: ");
    	gets(pass1);
    	if(strcmp(pass1,pass2)==0)
    	printf("Congrats!");
    	else
    	printf("Invalid Password %i Tries\n");
    	}
    	while((strcmp(pass1,pass2)!=0 || i!=3);
    getche();
    }
    This codes suppose to check password entered if it matches the given. If it is incorrect the loop will not terminate till one of the conditions below is satisfied... Help me out guys... Thanks in advance...

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    51
    I'm only starting out myself but you may get more suggestions if you say what the error is you are experiencing

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You probably want && instead of ||.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Code:
    #include <stdio.h>
    #include <string.h>
    main()
    {
    char pass1[20];
    char pass2[]= "password";
    int i=0;
    	do
    	{
    	i++;
    	clrscr();
    	printf("Enter Password: ");
    	gets(pass1); //use fgets instead
    	if(strcmp(pass1,pass2)==0)
    	printf("Congrats!");
    	else
    	printf("Invalid Password %i Tries\n");
    	}
    	while((strcmp(pass1,pass2))!=0 && i!=3);
    getche();
    }
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Code:
    printf("Invalid Password %d Tries\n", i);
    Also, neither getche() or clrscr() is part of stdio.h or string.h, or the C standard for that matter.
    Last edited by Subsonics; 11-22-2009 at 10:48 AM.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    12
    Thanks for the immediate response guys... Tried removing the clrscr() and it shows in the execution of the program 1277 tries... Why it exceeds the condition i!=3? Thanks in advance....

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    12
    I tried it again and the code works now... My question now is how do we clear the screen after the first trial are made? example:

    Enter Password: dasdadad //entered incorrect password
    Invalid Password 1 Tries //Then Clears the screen after showing the number of tries

    Enter Password: //This will show up only 3 times until the correct password or 3 trials are met.


    Thanks guys... ^_^

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by jappy512 View Post
    I tried it again and the code works now... My question now is how do we clear the screen after the first trial are made? example:

    Enter Password: dasdadad //entered incorrect password
    Invalid Password 1 Tries //Then Clears the screen after showing the number of tries

    Enter Password: //This will show up only 3 times until the correct password or 3 trials are met.


    Thanks guys... ^_^
    The screen is cleared so fast you can't see the invalid password message.

    The real problem, of course, is that anyone looking, can see what the user is keyboarding in as their password.

    The only two ways I know to solve that, are:

    1) Include the windows.h header and use the Windows API, and SetConsoleMode() to set the monitor to no echo mode, while the password is being entered (and you can use SetConsoleCursorPosition() also, as you want to).

    2) go back to using the DOS style console cursor control and control of the keyboard input to the terminal - getch() gets a key with no echo to the monitor, and gotoxy() will set the cursor to any place on the console window.

    It would be good if the password was encrypted on a file, also.

    There are other ways to do this, but they involve a little bit of another language, not C. Note that while #2 is included, it is not using standard C, and wouldn't be portable with newer compilers.
    Last edited by Adak; 11-23-2009 at 02:51 AM.

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    12
    Thanks to all the reply guys.... It helped me a lot and now the program runs smoothly because of all your suggestions..

    Thank You Very Much.. ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in multi times table
    By lolguy in forum C Programming
    Replies: 11
    Last Post: 12-28-2008, 11:17 AM
  2. Problem reading a password from a file.
    By medeshago in forum C Programming
    Replies: 15
    Last Post: 12-21-2008, 07:20 AM
  3. problem: online on winxp and linux red hat
    By gemini_shooter in forum Linux Programming
    Replies: 5
    Last Post: 05-29-2005, 02:14 PM
  4. Problem reading from external file
    By djayz in forum C Programming
    Replies: 13
    Last Post: 03-24-2005, 01:15 PM
  5. i have problem with my code...
    By jayton in forum C Programming
    Replies: 5
    Last Post: 02-15-2005, 03:10 PM