-
problems with getch()
There's a problem using getch in the following program. I can't quite figure out how to take the input, compile and run it yourself, it was compiled with turbo c 2.01...so some of the includes might be different in other compilers. The two strings will never equal eachother...just take a look at the output of what you type when you run the program and you'll see what I mean. Thanks.
Code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <dos.h>
#include <process.h>
int i=0;
int goAhead=1;
int hackTry=0;
char password[]="bcannadian";
char pword[50];
int main()
{
system("cls");
printf("Login V1.0\nCreated By Drew Peterson\n\n");
while(goAhead==1)
{
printf("Enter Password:");
for (i=0;i<50;i++)
{
pword[i]=getch();
if(pword[i]=='\r')
{
pword[i-1]='/0';
break;
}
fflush(stdin);
/* printf("*");*/
printf(pword);
}
if(strcmp(password,pword)==0)
{
goAhead=0;
}
else
{
printf("Incorrect Password\n\n");
hackTry=hackTry+1;
if(hackTry>=5)
{
printf("You Have Entered An Incorrect Password Too Many Times,\nPress Any Key To Reboot");
outportb(0xCF9,4);
outportb(0xCF9,2);
}
}
}
printf("You Have Successfully Logged In\nPress Any Key To Continue\n");
getch();
execl("c:\\com.com",0);
/*will only get to this point if it failed*/
printf("Failure To Load Operating System, Press Any Key To Reboot");
getch();
/*code to reboot*/
outportb(0xCF9,4);
outportb(0xCF9,2);
exit(0);
}
-
Why you want to
get char by char with getch() and then add them together.
Why won't you try using fgets, instead doing the whole job? :confused:
and why you use global variables? it's not needed in this case..
-
look here...
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 50 //max length of the password
int main(void)
{
char *pass = "hello"; //lol, our password.
char *usInput;
int i;
for(i=0;i<MAX;++i)
{
usInput[i] = getch();
if(usInput[i] == '\r') {
usInput[i] = '\0';
break;
}
fflush(stdin); //fflush the input stream
printf("*");
}
printf("The pass: %s\n",usInput);
if(strcmp(usInput,pass) == 0)
printf("Correct, you can go now.\n");
return 0;
}
You error was at this line
Code:
if(usInput[i] == '\r') {
usInput[i] = '\0';
break;
}
In this case you dont need to do [i-1].
Good luck.
-
yeah i had some dumb mistakes in there, but it's all been fixed this morning....even added in handling for backspaces. Anywho thanks guys.