We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,613 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

C++ help putting password, change password and delete

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();
}
2
Contributors
2
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
3
Views
Question
Answered
Nines
Newbie Poster
2 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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..

mridul.ahuja
Light Poster
25 posts since Oct 2010
Reputation Points: 2
Solved Threads: 1
Skill Endorsements: 0

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...

Nines
Newbie Poster
2 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by mridul.ahuja

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0767 seconds using 2.76MB