how do i put a password, change password on my code because everytime i put it in, my program don't shows it it just jumping to my selection menu, and how do i delete informations that ive put on my program? this is my code.

thank you in advance.

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <dos.h>
#include <stdlib.h>
#define p printf
#define s scanf

struct records
{
	char name[20],address[50], contact[12];
	int recno;
} rec;

void addemp();
void display();

FILE *emp;

void main()
{
	char choice;
	do
	{
		clrscr();
		p("[A] - Add Record");
		p("\n[D] - Display Record");
		p("\n[X] - Exit");
		p("\nEnter Choice: ");
		choice=toupper(getch());
		switch(choice)
		{
			case 'A':
				addemp();
				break;
			case 'D':
				display();
				break;
			case 'X':
				exit(0);
				break;
			default:
				p("Invalid Input");
		}
	}while(choice!='X');
}

void addemp()
{
	if ((emp=fopen("d:\\emp.txt","a+"))==NULL)
	{
		p("Handled File");
		delay(500);
	}
	else
	{
		while(!feof(emp))
		{
			fread(&rec.name,sizeof(char),20,emp);
			fread(&rec.address,sizeof(char),20,emp);
			fread(&rec.contact,sizeof(char),20,emp);
			fread(&rec.recno,sizeof(int),20,emp);
		}
		clrscr();
		rec.recno=rec.recno+1;
		p("Record no.: %.4d\n",rec.recno);
		p("Name: ");
		gets(rec.name);fflush(stdin);
		p("Address: ");
		gets(rec.address);fflush(stdin);
		p("Contact: ");
		gets(rec.contact);fflush(stdin);

		fwrite(&rec.name,sizeof(char),20,emp);
		fwrite(&rec.address,sizeof(char),20,emp);
		fwrite(&rec.contact,sizeof(char),20,emp);
		fwrite(&rec.recno,sizeof(int),20,emp);

		clrscr();
		p("Record Saved");
		delay(500);
	}
	fclose(emp);
	main();
}

void display()
{
	if ((emp=fopen("d:\\emp.txt","r"))==NULL)
	{
		p("Handled File");
		delay(500);
	}
	else
	{
		while(!feof(emp))
		{
			clrscr();
			fread(&rec.name,sizeof(char),20,emp);
			fread(&rec.address,sizeof(char),20,emp);
			fread(&rec.contact,sizeof(char),20,emp);
			fread(&rec.recno,sizeof(int),20,emp);
			p("Record no.: %.4d",rec.recno);
			p("\nName: %s",rec.name);
			p("\nAddress: %s",rec.address);
			p("\nContact: %s",rec.contact);
			getch();
		}
	}
	fclose(emp);
	main();
}

Adding a Password To The Program :

char pass [ ] = { "your_password" },ch , enter[length_of_your_password] ;
int i = 0 ;
cout<<" Password : " ;
while( (( ch=getch() )!= '\n' ) && ( i < ( length_of_your_password - 1 ) ))
{
cout << "☻" ; // hiding your password with ☻
enter [ i ] = ch ;
i++ ;
}
enter[ i ] = '\0' ; // adding null character to end the entered password

if( strcmp( enter , pass )== 0 ) // comparing password entered with the actual one
{

your_program_here

}

else
{
cout<<" Invalid Password ! " ;
}


--------code ends-------

Maybe , for changing password , you will need to store it in a data file (file processing )
In that case you will need to compare the password entered with the 1 stored in the file..

Adding a Password To The Program :

char pass [ ] = { "your_password" },ch , enter[length_of_your_password] ;
int i = 0 ;
cout<<" Password : " ;
while( (( ch=getch() )!= '\n' ) && ( i < ( length_of_your_password - 1 ) ))
{
cout << "☻" ; // hiding your password with ☻
enter [ i ] = ch ;
i++ ;
}
enter[ i ] = '\0' ; // adding null character to end the entered password

if( strcmp( enter , pass )== 0 ) // comparing password entered with the actual one
{

your_program_here

}

else
{
cout<<" Invalid Password ! " ;
}


--------code ends-------

Maybe , for changing password , you will need to store it in a data file (file processing )
In that case you will need to compare the password entered with the 1 stored in the file..

thank you for the help though i figured it out how to put up a password.. though changing password, and delete is still my problem :sad:

by the way do you know how can i put a delete code for the items that i input on my program? it already makes files for the stored items though i don't get how the delete code works...

How do i reset the existing password in cpp and need that new password for starting the program everytime i start the program??
Please help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.