Thursday, December 15, 2011

Password program in C

It will take alphanumeric input and display '*' on  the screen. Backspace functionality is also there.


#include<stdio.h>
#include<conio.h>


int chk(int,int,int);
int main(void)
{  char c[20],ch;
   int i=-1,k=0,x=0,y=0;
   clrscr();
   printf("Enter Passwo:");
   x=wherex();
   y=wherey();
   do{


   ch=getch();
   if(ch=='\b')
   {
   if(chk(x,y,wherex()))
   {
   c[i]=' ';
   --i;
   }
   }
   else if(ch!='\r')
   {
       ++i;
   c[i]=ch;
   putch('*');
   }
   }while(ch!='\r');
    printf("\nPassword is:");
   while(k<=i) {
   printf("%c",c[k]);
   k++;
   }
   getch();
   return 0;
}
 int chk(int xOriginal,int yOriginal,int xCurrent)
 {
 if(xOriginal==xCurrent)
 return 0;
 gotoxy(--xCurrent,yOriginal);
  putch(' ');
  gotoxy(xCurrent,yOriginal);
  return 1;
 }

Saturday, September 10, 2011

Access private values outside class


Both private and public member variables reside in consecutive memory locations, so using pointer to member variable we can access private values even outside the class.
Take a look:
#include<iostream.h>
class A
{
public :
int x;

A()
{
cout<<"Enter a  b c:";
cin>>a>>b>>c;

}
private:
int a,b;
char c;
};
int main()
{
A ob,*p;
p=&ob;

int *y=&(p->x);  // y stores address of public member x

cout<<"\nInside main:\n";
cout<<"a="<<*++y;
cout<<"b="<<*++y;
cout<<"c="<<(char)*++y;

return 0;
}