C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-22-2009, 08:29 AM   #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...
jappy512 is offline   Reply With Quote
Old 11-22-2009, 10:01 AM   #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
Martin_T is offline   Reply With Quote
Old 11-22-2009, 10:34 AM   #3
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,643
You probably want && instead of ||.


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 11-22-2009, 10:37 AM   #4
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 803
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
BEN10 is offline   Reply With Quote
Old 11-22-2009, 10:43 AM   #5
Registered User
 
Join Date: Jan 2009
Posts: 224
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.
Subsonics is offline   Reply With Quote
Old 11-22-2009, 05:35 PM   #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....
jappy512 is offline   Reply With Quote
Old 11-22-2009, 05:45 PM   #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... ^_^
jappy512 is offline   Reply With Quote
Old 11-23-2009, 02:48 AM   #8
Registered User
 
Join Date: Sep 2006
Posts: 3,156
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.
Adak is offline   Reply With Quote
Old 11-23-2009, 04:57 PM   #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.. ^_^
jappy512 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:24 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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