problems with getch()

This is a discussion on problems with getch() within the C Programming forums, part of the General Programming Boards category; There's a problem using getch in the following program. I can't quite figure out how to take the input, compile ...

  1. #1
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001

    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);
    }
    Last edited by Waldo2k2; 01-21-2003 at 12:40 AM.
    PHP and XML
    Let's talk about SAX

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    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?

    and why you use global variables? it's not needed in this case..
    Last edited by Vber; 01-21-2003 at 02:30 AM.

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    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.

  4. #4
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    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.
    PHP and XML
    Let's talk about SAX

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  2. Replies: 5
    Last Post: 11-26-2002, 10:34 PM
  3. Problems with getch()
    By GrNxxDaY in forum C++ Programming
    Replies: 14
    Last Post: 08-12-2002, 03:11 AM
  4. Using getch()
    By pr0ficient in forum Linux Programming
    Replies: 2
    Last Post: 07-26-2002, 06:59 AM
  5. getch() acting up...
    By Govtcheez in forum C Programming
    Replies: 9
    Last Post: 08-18-2001, 01:56 PM

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