R
rAgAv
I'm working on my school project.
The program is a diary, where you can enter the day's events and save
it in a file.
It *was* very simple, until i tried to implement the password security.
My idea was to give a username and password to each user so that they
can keep their diary personal.
the bugs are in the functions "void join::jin()", "int join::ucntint()"
and "void join::id()".
the first one is the function which accepts the new user's details and
checks whether the entered usename already exists.
the second one is the function which initializes the total no. of users
each time the program starts.
the third one is the function which verifies whether the entered
username and password(by any old user) is available in the database(the
file) or not.
all the above mentioned functions don't seem to respond in the expected
manner.
//Program to write diary on specific date and store it in a file.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<fstream.h>
#include<dos.h>
int u,ucnt,nuchk,chk;
\*
'u' is current user's no.;
'ucnt' is total no. of users;
'nuchk' is the variable which stores '1'if the new user chooses
existing username(used in the function jin());
'chk' is the variable which stores '1' if entered username and password
are found in the database(used in the function id());
*/
struct dat //Structure to store the date of the entry
{
int year;
int month;
int day;
};
struct tim //Structure to store the time of the entry
{
int hour;
int min;
int sec;
};
class diary //Class to store all the details of the entry
{char str[1000];
public:
int u1;
dat e;
tim tn;
void in()
{
cout<<"Enter the diary.....\n";
cout<<"\nDATE\t: "<<e.day<<"."<<e.month<<"."<<e.year<<"\tTIME\t:
"<<tn.hour<<"."<<tn.min<<"."<<tn.sec<<"\n\n";
gets(str);
}
void out()
{
cout<<"\n\nDATE\t: "<<e.day<<"."<<e.month<<"."<<e.year<<"\tTIME\t:
"<<tn.hour<<"."<<tn.min<<"."<<tn.sec<<"\n\n";
cout<<str<<"\n________________________________________________________________";
}
int yr()
{return e.year;
}
int mn()
{return e.month;
}
int da()
{
return e.day;
}
}di;
class join : private diary //Class which contains the user
information
{
char uname[10];
char pass[10];
int uno;
public:
int match(char[],char[]);
int ucntint();
void admi();
void jin();
void id();
void retuname()
{
cout<<uname;
}
}jn;
void join::jin()
/*PROBLAMATIC #1 Function which accepts new user's desired username and
password anc checks whether chosen username already exists
or not*/
{ char un[10],chp;
cout<<"\nEnter desired username\t\t";
gets(un);
cout<<"\nEnter desired password\t\t";
gets(jn.pass);
uno=ucnt;
if(ucnt!=0)
{
ifstream fin("diary.dat",ios::binary);
while(!fin.eof())
{
nuchk=0;
fin.read((char*)&jn,sizeof(join));
{
for(int i=0;un!='\0';i++)
{ if(un==jn.uname)
nuchk=1;
else if(un!=jn.uname)
nuchk=0;
}
}
}
if(nuchk==1)
{
cout<<"\n\nThe chosen username already exists.";
cout<<"\n\nWould you like to choose another one?\t";
cin>>chp;
if(chp=='y'||chp=='Y')
{ clrscr();
jin();
}
}
else
strcpy(uname, un);
chk=1;
fin.close();
}}
int join::ucntint()
/*PROBLAMATIC #2 Function which initializes the total no. of users
every time the program starts.
*/
{int big=0;
ifstream fin("user.dat",ios::binary);
while(!fin.eof())
{
fin.read((char*)&jn,sizeof(join));
{
if(jn.uno>big)
{
big=jn.uno;
}}}
fin.close();
ucnt=big+1;
return 0;
}
void join::id()
/*PROBLAMATIC #2 Function which checks whether the username and
password entered by the old user is vald or not
*/
{char a[10],b[10],c='y';
while(c=='y')
{cout<<"\t\t\tUSER IDENTIFICATION\n\n";
cout<<"\t\t\t--------case sensitive-------";
cout<<"\n\n\t\t\tUSERNAME\t:\t";
gets(a);
cout<<"\n\t\t\tPASSWORD\t:\t";
gets(b);
ifstream fin("user.dat",ios::binary);
while(!fin.eof())
{ chk=0;
fin.read((char*)&jn,sizeof(join));
for(int i=0;a!='\0';i++)
{if(a==jn.uname)
nuchk=1;
else if(a!=jn.uname)
nuchk=0;
}
for(i=0;b!='\0';i++)
{
if(b==jn.pass)
nuchk=1;
else if(b!=jn.pass)
nuchk=0;
}
}
if(chk==1)
{cout<<"\nUSER VERIFIED";
u=jn.uno;
strcpy(jn.uname, a);
}
else
{
cout<<"\n\nINVALID USERNAME OR PASSWORD!!";
cout<<"\n\nWant to try again?";
cin>>c;
clrscr();
}
fin.close();
}}
void join::admi() //Function which writes the new
user's information onto the file
{
ofstream fout("user.dat",ios::binary|ios::app);
if(!fout)
cout<<"\n\nError!!The file has been erased or renamed";
else
{jn.jin();
fout.write((char*)&jn,sizeof(join));
}
fout.close();
}
void write() //Function to write the entry onto the file
{char rep='y',ch,str[100];
ofstream fout("diary.dat",ios::binary|ios::app);
if(!fout)
cout<<"\n\nError!!The file has been erased or renamed";
else
{while(rep=='y'||rep=='Y')
{
di.in();
fout.write((char*)&di,sizeof(diary));
rep='n';
}
fout.close();
}}
void read() //Function to read the entry from the file
{char ch;
ifstream fin("diary.dat",ios::binary);
if(!fin)
cout<<"\n\nError!!The file has been erased or renamed";
else
{while(!fin.eof())
{
fin.read((char*)&di,sizeof(diary));
if(di.u1==u)
di.out();
}fin.close();
}}
void readon() //Function to read from a specific date in the diary
{int yr,mn,da;
char ch;
ifstream fin("diary.dat");
if(!fin)
cout<<"Error!!";
else
{cout<<"Enter the date(Only numbers)\n";
cout<<"\nYear\t";
cin>>yr;
cout<<"\nMonth\t";
cin>>mn;
cout<<"\nDay\t";
cin>>da;
if(di.yr()==yr&&di.mn()==mn&&di.da()==da)
{
fin.read((char*)&di,sizeof(diary));
if(di.u1==u)
di.out();
}
else
{
cout<<"Invalid date";
}
}fin.close();
}
void erase() //Function to erase the entry on a specific date
{int yr,mn,da;
ifstream fin("diary.dat",ios::binary);
ofstream fout("dia.dat",ios::binary||ios::app);
if((!fin)||(!fout))
cout<<"Error!!";
else
{
cout<<"Enter the date of the entry to be erased\n";
cout<<"Year\t";
cin>>yr;
cout<<"\nMonth\t";
cin>>mn;
cout<<"\nDay\t";
cin>>da;
fin.read((char*)&di,sizeof(diary));
if(di.yr()!=yr&&di.mn()!=mn&&di.da()!=da)
{
fout.write((char*)&di,sizeof(diary));
}
remove ("diary.dat");
rename ("dia.dat","diary.dat");
}
fout.close();
fin.close();
}
void eraseall() //Fuction to create back up for the diary
{
rename("diary.dat","budiary.bu");
if(!rename("diary.dat","budiary.bu"))
cout<<"File back up unsuccessful";
else
cout<<"File successfully backed up";
}
void erasefe() //Function to erase the diary forever
{
remove("diary.dat");
if(remove("diary.dat")==0)
cout<<"Attempt unsuccessful";
else
cout<<"File successfully erased forever";
}
void bu() //Function to recover the back up file
{
remove("diary.dat");
rename("budiary.bu","diary.dat");
if(!rename("budiary.bu","diary.dat"))
cout<<"\n\nERROR!!The backed up file has been erased or renamed";
else
cout<<"\n\nFile successfully backed";
}
int main(void) //Main function
{clrscr();
ifstream fin("user.dat",ios::binary);
if(!fin)
ucnt=0;
else
jn.ucntint();
int ch,ch1,ch2,ch4;
char ch3;
cout<<"\t\t\t\t\t\tNo. of users"<<ucnt;
cout<<"\nUSER STATUS...\n\n1.NEW\n2.OLD\n\n";
cin>>ch4 ;
if(ch4==1)
{
jn.admi();
}
else if(ch4==2)
{jn.id();
}
else
cout<<"\nINVALID CHOICE!!";
do
{if(chk==1)
{clrscr();
cout<<"\t\t\t\t\tWELCOME ";
jn.retuname();
cout<<"!!";
cout<<"\n\nChoose from the following actions\n\n1.ENTER DIARY\n\n2.READ
DIARY\n\n3.ERASE\n";
cin>>ch;
if(ch==1)
{clrscr();
struct dosdate_t d; //Statements to get the system date
_dos_getdate(&d);
di.e.year=d.year;
di.e.month=d.month;
di.e.day=d.day;
struct dostime_t t; //Statements ro get the system time
_dos_gettime(&t);
di.tn.hour=t.hour;
di.tn.min=t.minute;
di.tn.sec=t.second;
write();
main();
}
else if(ch==2)
{clrscr();
cout<<"\n\n1.Read from specific date\n\n2.Show all\n\n";
cin>>ch1;
if(ch1==1)
{
readon();
cout<<"\n\nWould you like to reach the main menu?";
cin>>ch3;
}
else if(ch1==2);
{
read();
cout<<"\n\nWould you like to reach the main menu?";
cin>>ch3;
}
}
else
{
cout<<"Invalid choice";
}
if(ch==3)
{clrscr();
cout<<"\n\nChoose from the following erase functions\n\n1.Erase a
specific entry\n\n2.Erase the whole diary(but have a
backup)\n\n3.Erase the diary forever\n\n4.Retrieve backup(and erase
current diary)\n\n";
cin>>ch2;
switch(ch2)
{
case 1 :{erase();
break;
}
case 2 :{eraseall();
break;
}
case 3 :{erasefe();
break;
}
case 4 :{bu();
break;
}
default :{cout<<"Invalid choice!!";
break;
}
}
cout<<"\n\nWould you like to reach the main menu?(y/n)";
cin>>ch3;
}}
}while(ch3=='y'||ch3=='Y');
return 0;
}
/***********END*****************/
May be the way i tried to implement the password security is wrong or
there is some logical or runtime error.
I'd love to get some suggestions from pros.
The program is a diary, where you can enter the day's events and save
it in a file.
It *was* very simple, until i tried to implement the password security.
My idea was to give a username and password to each user so that they
can keep their diary personal.
the bugs are in the functions "void join::jin()", "int join::ucntint()"
and "void join::id()".
the first one is the function which accepts the new user's details and
checks whether the entered usename already exists.
the second one is the function which initializes the total no. of users
each time the program starts.
the third one is the function which verifies whether the entered
username and password(by any old user) is available in the database(the
file) or not.
all the above mentioned functions don't seem to respond in the expected
manner.
//Program to write diary on specific date and store it in a file.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<fstream.h>
#include<dos.h>
int u,ucnt,nuchk,chk;
\*
'u' is current user's no.;
'ucnt' is total no. of users;
'nuchk' is the variable which stores '1'if the new user chooses
existing username(used in the function jin());
'chk' is the variable which stores '1' if entered username and password
are found in the database(used in the function id());
*/
struct dat //Structure to store the date of the entry
{
int year;
int month;
int day;
};
struct tim //Structure to store the time of the entry
{
int hour;
int min;
int sec;
};
class diary //Class to store all the details of the entry
{char str[1000];
public:
int u1;
dat e;
tim tn;
void in()
{
cout<<"Enter the diary.....\n";
cout<<"\nDATE\t: "<<e.day<<"."<<e.month<<"."<<e.year<<"\tTIME\t:
"<<tn.hour<<"."<<tn.min<<"."<<tn.sec<<"\n\n";
gets(str);
}
void out()
{
cout<<"\n\nDATE\t: "<<e.day<<"."<<e.month<<"."<<e.year<<"\tTIME\t:
"<<tn.hour<<"."<<tn.min<<"."<<tn.sec<<"\n\n";
cout<<str<<"\n________________________________________________________________";
}
int yr()
{return e.year;
}
int mn()
{return e.month;
}
int da()
{
return e.day;
}
}di;
class join : private diary //Class which contains the user
information
{
char uname[10];
char pass[10];
int uno;
public:
int match(char[],char[]);
int ucntint();
void admi();
void jin();
void id();
void retuname()
{
cout<<uname;
}
}jn;
void join::jin()
/*PROBLAMATIC #1 Function which accepts new user's desired username and
password anc checks whether chosen username already exists
or not*/
{ char un[10],chp;
cout<<"\nEnter desired username\t\t";
gets(un);
cout<<"\nEnter desired password\t\t";
gets(jn.pass);
uno=ucnt;
if(ucnt!=0)
{
ifstream fin("diary.dat",ios::binary);
while(!fin.eof())
{
nuchk=0;
fin.read((char*)&jn,sizeof(join));
{
for(int i=0;un!='\0';i++)
{ if(un==jn.uname)
nuchk=1;
else if(un!=jn.uname)
nuchk=0;
}
}
}
if(nuchk==1)
{
cout<<"\n\nThe chosen username already exists.";
cout<<"\n\nWould you like to choose another one?\t";
cin>>chp;
if(chp=='y'||chp=='Y')
{ clrscr();
jin();
}
}
else
strcpy(uname, un);
chk=1;
fin.close();
}}
int join::ucntint()
/*PROBLAMATIC #2 Function which initializes the total no. of users
every time the program starts.
*/
{int big=0;
ifstream fin("user.dat",ios::binary);
while(!fin.eof())
{
fin.read((char*)&jn,sizeof(join));
{
if(jn.uno>big)
{
big=jn.uno;
}}}
fin.close();
ucnt=big+1;
return 0;
}
void join::id()
/*PROBLAMATIC #2 Function which checks whether the username and
password entered by the old user is vald or not
*/
{char a[10],b[10],c='y';
while(c=='y')
{cout<<"\t\t\tUSER IDENTIFICATION\n\n";
cout<<"\t\t\t--------case sensitive-------";
cout<<"\n\n\t\t\tUSERNAME\t:\t";
gets(a);
cout<<"\n\t\t\tPASSWORD\t:\t";
gets(b);
ifstream fin("user.dat",ios::binary);
while(!fin.eof())
{ chk=0;
fin.read((char*)&jn,sizeof(join));
for(int i=0;a!='\0';i++)
{if(a==jn.uname)
nuchk=1;
else if(a!=jn.uname)
nuchk=0;
}
for(i=0;b!='\0';i++)
{
if(b==jn.pass)
nuchk=1;
else if(b!=jn.pass)
nuchk=0;
}
}
if(chk==1)
{cout<<"\nUSER VERIFIED";
u=jn.uno;
strcpy(jn.uname, a);
}
else
{
cout<<"\n\nINVALID USERNAME OR PASSWORD!!";
cout<<"\n\nWant to try again?";
cin>>c;
clrscr();
}
fin.close();
}}
void join::admi() //Function which writes the new
user's information onto the file
{
ofstream fout("user.dat",ios::binary|ios::app);
if(!fout)
cout<<"\n\nError!!The file has been erased or renamed";
else
{jn.jin();
fout.write((char*)&jn,sizeof(join));
}
fout.close();
}
void write() //Function to write the entry onto the file
{char rep='y',ch,str[100];
ofstream fout("diary.dat",ios::binary|ios::app);
if(!fout)
cout<<"\n\nError!!The file has been erased or renamed";
else
{while(rep=='y'||rep=='Y')
{
di.in();
fout.write((char*)&di,sizeof(diary));
rep='n';
}
fout.close();
}}
void read() //Function to read the entry from the file
{char ch;
ifstream fin("diary.dat",ios::binary);
if(!fin)
cout<<"\n\nError!!The file has been erased or renamed";
else
{while(!fin.eof())
{
fin.read((char*)&di,sizeof(diary));
if(di.u1==u)
di.out();
}fin.close();
}}
void readon() //Function to read from a specific date in the diary
{int yr,mn,da;
char ch;
ifstream fin("diary.dat");
if(!fin)
cout<<"Error!!";
else
{cout<<"Enter the date(Only numbers)\n";
cout<<"\nYear\t";
cin>>yr;
cout<<"\nMonth\t";
cin>>mn;
cout<<"\nDay\t";
cin>>da;
if(di.yr()==yr&&di.mn()==mn&&di.da()==da)
{
fin.read((char*)&di,sizeof(diary));
if(di.u1==u)
di.out();
}
else
{
cout<<"Invalid date";
}
}fin.close();
}
void erase() //Function to erase the entry on a specific date
{int yr,mn,da;
ifstream fin("diary.dat",ios::binary);
ofstream fout("dia.dat",ios::binary||ios::app);
if((!fin)||(!fout))
cout<<"Error!!";
else
{
cout<<"Enter the date of the entry to be erased\n";
cout<<"Year\t";
cin>>yr;
cout<<"\nMonth\t";
cin>>mn;
cout<<"\nDay\t";
cin>>da;
fin.read((char*)&di,sizeof(diary));
if(di.yr()!=yr&&di.mn()!=mn&&di.da()!=da)
{
fout.write((char*)&di,sizeof(diary));
}
remove ("diary.dat");
rename ("dia.dat","diary.dat");
}
fout.close();
fin.close();
}
void eraseall() //Fuction to create back up for the diary
{
rename("diary.dat","budiary.bu");
if(!rename("diary.dat","budiary.bu"))
cout<<"File back up unsuccessful";
else
cout<<"File successfully backed up";
}
void erasefe() //Function to erase the diary forever
{
remove("diary.dat");
if(remove("diary.dat")==0)
cout<<"Attempt unsuccessful";
else
cout<<"File successfully erased forever";
}
void bu() //Function to recover the back up file
{
remove("diary.dat");
rename("budiary.bu","diary.dat");
if(!rename("budiary.bu","diary.dat"))
cout<<"\n\nERROR!!The backed up file has been erased or renamed";
else
cout<<"\n\nFile successfully backed";
}
int main(void) //Main function
{clrscr();
ifstream fin("user.dat",ios::binary);
if(!fin)
ucnt=0;
else
jn.ucntint();
int ch,ch1,ch2,ch4;
char ch3;
cout<<"\t\t\t\t\t\tNo. of users"<<ucnt;
cout<<"\nUSER STATUS...\n\n1.NEW\n2.OLD\n\n";
cin>>ch4 ;
if(ch4==1)
{
jn.admi();
}
else if(ch4==2)
{jn.id();
}
else
cout<<"\nINVALID CHOICE!!";
do
{if(chk==1)
{clrscr();
cout<<"\t\t\t\t\tWELCOME ";
jn.retuname();
cout<<"!!";
cout<<"\n\nChoose from the following actions\n\n1.ENTER DIARY\n\n2.READ
DIARY\n\n3.ERASE\n";
cin>>ch;
if(ch==1)
{clrscr();
struct dosdate_t d; //Statements to get the system date
_dos_getdate(&d);
di.e.year=d.year;
di.e.month=d.month;
di.e.day=d.day;
struct dostime_t t; //Statements ro get the system time
_dos_gettime(&t);
di.tn.hour=t.hour;
di.tn.min=t.minute;
di.tn.sec=t.second;
write();
main();
}
else if(ch==2)
{clrscr();
cout<<"\n\n1.Read from specific date\n\n2.Show all\n\n";
cin>>ch1;
if(ch1==1)
{
readon();
cout<<"\n\nWould you like to reach the main menu?";
cin>>ch3;
}
else if(ch1==2);
{
read();
cout<<"\n\nWould you like to reach the main menu?";
cin>>ch3;
}
}
else
{
cout<<"Invalid choice";
}
if(ch==3)
{clrscr();
cout<<"\n\nChoose from the following erase functions\n\n1.Erase a
specific entry\n\n2.Erase the whole diary(but have a
backup)\n\n3.Erase the diary forever\n\n4.Retrieve backup(and erase
current diary)\n\n";
cin>>ch2;
switch(ch2)
{
case 1 :{erase();
break;
}
case 2 :{eraseall();
break;
}
case 3 :{erasefe();
break;
}
case 4 :{bu();
break;
}
default :{cout<<"Invalid choice!!";
break;
}
}
cout<<"\n\nWould you like to reach the main menu?(y/n)";
cin>>ch3;
}}
}while(ch3=='y'||ch3=='Y');
return 0;
}
/***********END*****************/
May be the way i tried to implement the password security is wrong or
there is some logical or runtime error.
I'd love to get some suggestions from pros.