Need some help !!!


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

Advertisements

A

Alf P. Steinbach

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

May I suggest using descriptive names so that you don't have to explain
what the functions do.

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>

Non-standard header.

#include<string.h>

You'll find it much easier to use std::string.

#include<stdio.h>
#include<fstream.h>

You'll find it much easier to use standard iostreams.

#include<dos.h>

Non-standard header.

int u,ucnt,nuchk,chk;

Global variables -- remove them.

Also, I suggest using descriptive names.


[snip]
class join : private diary //Class which contains the user information
{

Are you sure that each 'join' instance is (secretly) a diary?

Also, I suggest using descriptive names.

Hth.,

- Alf
 
R

rAgAv

You'll find it much easier to use standard iostreams.

Would that help remove the bugs? I don't know much about "standard
iostreams".

Non-standard header.



Global variables -- remove them.
Alternatives?


Also, I suggest using descriptive names.

Yes sir! I will.
But not now.
DEAD LINE IS NEARING!!!

i desperately need some debugging.
Are you sure that each 'join' instance is (secretly) a diary?

i've used the global varialble 'u' in which a unique user no. for each
username and password combination is stored. Every time the user logs
in with a password, the global variable 'u' is made to hold the value
of user no. And when the user requests to open his diary, only those
objects of class 'Diary' which hold user no.= to u are displayed.
variable di.d1(element in the class 'diary') is the one in which the
user no. is stored.

The checking is done in respective file in[put and file output
functions(i.e read(), and write()).

The idea might seem good,but the implementation is bad.
 
S

Stuart Redmann

rAgAv said:
Would that help remove the bugs? I don't know much about "standard
iostreams".

It would not remove any bugs, but make your source code compilable under
other platforms (for example, I cannot compile this code under MS VC 7.0
because you use the non-standard header fstream.h. Had you used fstream
instead, I might have been able to help you).
Yes sir! I will.
But not now.
DEAD LINE IS NEARING!!!

That is not our problem. You cannot reasonably expect everybody to spend
much time with getting acquainted with the garbled names you use
throughout your code. I've seen many projects that have been abandoned
because they weren't readable anymore (after some 100,000$ have been fed
into the development). Anyway, your instructor will much likely reject
badly documented code.

Stuart
 
L

LR

rAgAv said:
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.

What did you expect? What happened instead?

//Program to write diary on specific date and store it in a file.

<snip non standard headers. The other posters to this thread are
correct, you shouldn't be using those>
int u,ucnt,nuchk,chk;

These are global because? Is there ever going to be more than one
instance of the file user.dat used by your program at run time?

<snip 'comment' body, although I'm pretty sure it's not a real comment
because that doesn't look the way the start of a comment normally looks.
You're best off posting code that will actually compile>


<snip the structs 'dat' and 'tim'. I'm not sure why you didn't call
them 'date' and 'time'.>
class diary //Class to store all the details of the entry
{char str[1000];

May I ask why you're not using std::string?

int yr()
{return e.year;
}

The above member function probably shouldn't be a member of diary.
Probably you'd want a member function of diary that returns a 'date',
and then you'd want a member function of date 'int year() const;' or
something similar.

<snip the other two functions like this one which should probably be
members of 'date'.

class join : private diary //Class which contains the user
information

So 'join' is-a 'diary'? Why is that?

{
char uname[10];
char pass[10];

Another great place to use std::string.

}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);

May I ask why you're using binary?

Do you expect to find some information about the user, like their
password and username in 'diary.dat'? What other information will be in
that file?



That's about all I have time for, so:
<snip the rest of the code>


May be the way i tried to implement the password security is wrong or
there is some logical or runtime error.
Undoubtedly.


I'd love to get some suggestions from pros.

Of course, you've been given some great ones in this thread already.
Like use better function names. And probably don't use so much global
data.

I'd add, make each function do one thing. It might be better to have a
function ask for a name and password pair and another that looks the
pair up to see if it's valid.

Try to test each of these on it's own.

Maybe also, don't hard code those filenames everywhere. A member
function of your class join might return the name of the file like so:
static std::string userDataFileName() {
return "filename.txt";
}
with the added benefit that if you want to try the code with a different
file, you can more easily change the name.

And why are you using binary?


thanks in advance,


You are welcome.
 
R

rAgAv

LR said:
rAgAv wrote:
What did you expect? What happened instead?

Try running the program in TC.
<snip non standard headers. The other posters to this thread are
correct, you shouldn't be using those>

Tell me what i *should be doing*.". Because i'm not very sure what
standard headers are.
int u,ucnt,nuchk,chk;

These are global because? Is there ever going to be more than one
instance of the file user.dat used by your program at run time?

<snip 'comment' body, although I'm pretty sure it's not a real comment
because that doesn't look the way the start of a comment normally looks.
You're best off posting code that will actually compile>


<snip the structs 'dat' and 'tim'. I'm not sure why you didn't call
them 'date' and 'time'.>
class diary //Class to store all the details of the entry
{char str[1000];

May I ask why you're not using std::string?
Cause i'm not a professional and not quite sure what they are.May be i
know what they are but just can't associate them with the term
std::string. I've just been learnig c++ for the past one year.
int yr()
{return e.year;
}

The above member function probably shouldn't be a member of diary.
Probably you'd want a member function of diary that returns a 'date',
and then you'd want a member function of date 'int year() const;' or
something similar.

<snip the other two functions like this one which should probably be
members of 'date'.

class join : private diary //Class which contains the user
information

So 'join' is-a 'diary'? Why is that?
{
char uname[10];
char pass[10];

Another great place to use std::string.

}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);

May I ask why you're using binary?

I thought using binary would encrypt the data and write so that ppl
offline cannot read it. I was obviously wrong!
What other information will be in
that file?

Apart from username and password, the user no.(i.e a unique no. for
each username and password combination) and the actual diary entry,
date and time of entry are present in the file.
That's about all I have time for, so:
<snip the rest of the code>

Thanks for spending your time on this.
Of course, you've been given some great ones in this thread already.
Like use better function names. And probably don't use so much global
data.

I'd add, make each function do one thing. It might be better to have a
function ask for a name and password pair and another that looks the
pair up to see if it's valid.

Try to test each of these on it's own.

Maybe also, don't hard code those filenames everywhere. A member
function of your class join might return the name of the file like so:
static std::string userDataFileName() {
return "filename.txt";
}
with the added benefit that if you want to try the code with a different
file, you can more easily change the name.

You're right.

May be i just need some reading of my text book to get a better
understanding of the concepts. I just did read it and got some ideas.

Anyway thanks for the advice.
 
Ad

Advertisements

R

rAgAv

Somebody please tell me more about standard headers. I think it'll be
very useful because it makes my program compatible for all platforms.

Thanks in advance
 
R

rAgAv

Somebody please tell me more about standard headers. I think it'll be
very useful because it makes my program compatible for all platforms.

Thanks in advance
 
L

LR

rAgAv said:
Try running the program in TC.

Tempting, but no. I have no way of knowing what your code is supposed to
do, so I believe that it's your responsibility to describe the problem
you are experiencing if you want help.


Tell me what i *should be doing*.". Because i'm not very sure what
standard headers are.

For example, <iostream.h> is not standard, <iostream> is.




Cause i'm not a professional and not quite sure what they are.May be i
know what they are but just can't associate them with the term
std::string. I've just been learnig c++ for the past one year.

Does the compiler you're using have an IDE with integrated help? Does
the book you're using (and may I ask what your text is?) have an entry
in the index for 'string', 'std' and/or 'std::string'? If not, then
perhaps you could make good use of google to look these up.




I'd still like an answer to this.


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);

May I ask why you're using binary?


I thought using binary would encrypt the data and write so that ppl
offline cannot read it. I was obviously wrong!

Then I suggest that you look up encrypt.


Apart from username and password, the user no.(i.e a unique no. for
each username and password combination) and the actual diary entry,
date and time of entry are present in the file.

Then I am confused by your code. If this is what the diary.dat file is
for, what is the user.dat file for? If all you're doing in this
function is checking for the existence of a name pw pair, then which
file should you be checking? Or does it make a difference?

I tend to think that the way you've inherited from diary is confusing
the issue somewhat.


Thanks for spending your time on this.

You're welcome.
May be i just need some reading of my text book to get a better
understanding of the concepts. I just did read it and got some ideas.

Very good idea. But if I may repeat myself. Could you please tell us
what text book you're using?

Anyway thanks for the advice.

And if I may repeat myself again, you're welcome.

LR
 
R

rAgAv

Could you please tell us
what text book you're using?

If you're not in India, you may not know this book. But anyway, its
name is "Computer science for XII std. by Sumita Arora" and i'm using
the "Turbo c++ IDE compiler".


i don't think i can use the standard iostream(i.e #include<iostream>)
my compiler says "cannot open the file 'iostream' "

I've finished debugging the program and now, just a tiny bug
left.Otherwise, its submit-worthy.

If you'd like to see the code, here it is:-

//Program to write diary on specific date and store it in a file.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdio.h>
#include<fstream.h>
#include<dos.h>
int ucnt,u;
/*
'u' is current user's no.;
'ucnt' is total no. of users;
Both the variable are global because,
they are initialized every time the program starts.
*/
struct date dd;
struct time tt;
struct entrydate //Structure to store the date of the entry
{
int year,month,day;
};
struct entrytime
{
int hour,min,sec;
};
class diary //Class to store all the details of the entry
{
unsigned long char str[1000];
int u1;
public:
entrydate e;
entrytime tn;
int retu1()
{
return u1;
}
void in();
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 user //Class which contains the user information
{
char uname[10],pass[10];
int uno;
public:
int a;
int usercountinitialize();
void admit();
void checknewuser(class user);
void unoinitialize(int a)
{
uno=a;
}
void getnewuser()
{
clrscr();
cout<<"\nEnter desired username\t\t";
gets(uname);
cout<<"\nEnter desired password\t\t";
gets(pass);
a=ucnt+1;
unoinitialize(a);
}
void getuser()
{
clrscr();
cout<<"\t\t\tUSER IDENTIFICATION\n\n";
cout<<"\t\t\t--------case sensitive-------";
cout<<"\n\n\t\t\tUSERNAME\t:\t";
gets(uname);
cout<<"\n\t\t\tPASSWORD\t:\t";
gets(pass);
}
void verifyid();
void retuname()
{
cout<<uname;
}
int retuno()
{
return uno;
}
}jn;

void diary::in()
{
getdate(&dd);
di.e.year=dd.da_year;
di.e.month=dd.da_mon;
di.e.day=dd.da_day;
gettime(&tt);
di.tn.hour=tt.ti_hour;
di.tn.min=tt.ti_min;
di.tn.sec=tt.ti_sec;
di.u1=jn.retuno();
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);
if(u==1)
u1=1;
else
u1=u;
}

void user::checknewuser(user j1) /*Function which accepts new user's
username and password and
checks whether chosen username already exists or not*/
{ char chp,c='y';
int nuchk;
if(ucnt==0)
c='n';
while(c=='y')
{
ifstream fin("user.dat",ios::binary);
fin.seekg(0);
while(!fin.eof())
{
nuchk=0;
fin.read((char*)&jn,sizeof(user));
if((strcmpi(jn.uname,j1.uname))==0)
{
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')
{
j1.getnewuser();
checknewuser(j1);
break;
}
else
exit(0);
}
}
if(nuchk==0)
{
jn=j1;
c='n';
break;
}
fin.close();
}
}

int user::usercountinitialize() /*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(user));
if(jn.retuno()>big)
{
big=jn.retuno();
}
}
fin.close();
ucnt=big;
return 0;
}

void user::verifyid() /*Function which checks
whether entered id is valid or not*/
{
char c='y';
int c1;
while(c=='y')
{
user j2;
j2.getuser();
ifstream fin("user.dat",ios::binary);
fin.seekg(0);
while(!fin.eof())
{
c1=0;
fin.read((char*)&jn,sizeof(user));
if((strcmpi(j2.uname,jn.uname))==0&&(strcmpi(j2.pass,jn.pass))==0)
{
c='n';
c1=1;
cout<<"\nUSER VERIFIED";
u=jn.retuno();
break;
}
}
if(c1==0)
{
cout<<"\n\nINVALID USERNAME OR PASSWORD!!";
cout<<"\n\nWant to try again?";
cin>>c;
if(c=='n')
exit(0);
}
fin.close();
}
}

void user::admit() /*Function which writes the
new user's information onto the file*/
{
user j1;
ofstream fout("user.dat",ios::binary|ios::app);
if(!fout)
{
cout<<"\n\nError!!The file has been erased or renamed";
exit(0);
}
else
{
j1.getnewuser();
jn.checknewuser(j1);
fout.write((char*)&j1,sizeof(user));
}
fout.close();
}


void write() //Function to write the entry onto the file
{
char rep='y';
unsigned long char str[1000];
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
{
fin.seekg(0);
while(!fin.eof())
{
fin.read((char*)&di,sizeof(diary));
if(di.retu1()==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",ios::binary);
if(!fin)
cout<<"Error!!The file has been erased or renamed";
else
{
fin.seekg(0);
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.retu1()==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!!The file has been deleted or renamed";
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&&di.retu1()==u)
{
fout.write((char*)&di,sizeof(diary));
}
remove ("diary.dat");
rename ("dia.dat","diary.dat");
}
fout.close();
fin.close();
}
void eraseall() //Function to erase the entire diary
{
int yr,mn,da;
ifstream fin("diary.dat",ios::binary);
ofstream fout("dia.dat",ios::binary,ios::app);
if((!fin)||(!fout))
cout<<"Error!!The file has been deleted or renamed";
else
{
fin.read((char*)&di,sizeof(diary));
if(di.retu1()==u)
{
fout.write((char*)&di,sizeof(diary));
}
remove ("diary.dat");
rename ("dia.dat","diary.dat");
}
fout.close();
fin.close();
}

void main() //Main function
{
clrscr();
int ch,ch1,ch2,ch4,chk;
char ch3;
ifstream fin("user.dat",ios::binary);
if(!fin)
{
ucnt=0;
fin.close();
}
else
{ fin.close();

jn.usercountinitialize();
}
gotoxy(35,0);
cout<<"USER COUNT\t"<<ucnt;
gotoxy(25,15);
cout<<"CHOOSE FROM THE FOLLOWING FUNCTIONS:-";
gotoxy(25,17);
cout<<"1.USER SIGN UP";
gotoxy(25,19);
cout<<"2.USER LOGIN";
gotoxy(25,21);
cout<<"3.EXIT\t";
cin>>ch4 ;
switch(ch4)
{
case 1 :{
jn.admit();
chk=1;
jn.unoinitialize(ucnt+1);
break;
}
case 2 :{
ifstream fin("user.dat",ios::binary);
if(!fin)
{
cout<<"User file is deleted or renamed!";
exit(0);
break;
}
else
{
jn.verifyid();
chk=1;
break;
}
}
case 3 :{
exit(0);
break;
}
default :{
cout<<"\nINVALID CHOICE!!";
break;
}
}

if(chk==1)
{
do
{
clrscr();
cout<<"\t\t\t\tWELCOME ";
jn.retuname();
cout<<"\n\nChoose from the following actions\n\n1.ENTER
DIARY\n\n2.READ DIARY\n\n3.ERASE\n\n4.EXIT\n\n";
cin>>ch;
switch(ch)
{
case 1 :{
clrscr();
write();
ch3='y';
break;
}
case 2 :{
clrscr();
cout<<"\n\n1.Read from specific date\n\n2.Show all\n\n";
cin>>ch1;
switch(ch1)
{
case 1 :{
readon();
cout<<"\n\nWould you like to reach ";
jn.retuname();
cout<<"'s menu?(y/n)";
cin>>ch3;
break;
}
case 2 :{
u=jn.retuno();
read();
cout<<"\n\nWould you like to reach ";
jn.retuname();
cout<<"'s menu?(y/n)";
cin>>ch3;
}
default :{
cout<<"\n\nINVALID CHOICE";
break;
}
}
}
case 3 :{
clrscr();
cout<<"\n\nChoose from the following erase functions\n\n1.Erase a
specific entry\n\n2.Erase the whole diary\n\n";
cin>>ch2;
switch(ch2)
{
case 1 :{
erase();
break;
}
case 2 :{
eraseall();
break;
}
default :{
cout<<"INVALID CHOICE!!";
break;
}
}
}
case 4 :{
exit(0);
break;
}
default :{
cout<<"\n\nINVALID CHOICE";
break;
}

}
}while(ch3=='y'||ch3=='Y');
}
getch();
}


/**************END*****************/
 
Ad

Advertisements

P

Puppet_Sock

rAgAv said:
If you're not in India, you may not know this book. But anyway, its
name is "Computer science for XII std. by Sumita Arora" and i'm using
the "Turbo c++ IDE compiler".

You want _Accelerated C++_ by Koenig and Moo. You want it very much.
Learn C++ the right way, the first time. Then you won't have a lot of
bad
habits to unlearn later. Read this book and, in two or three weeks,
you
will be writing far better C++ code.

Possibly there are better compilers you can download for free. How old
is that TC++ compiler? If it's ten years old or so, I can see why it
would
not have the standard header files. But you should be aware that C++
has had some changes in the last while, and you are missing a lot if
you have a very old compiler. You could look into the Gnu compiler.
Search at google for it.
Socks
 
R

rAgAv

MY SUBMITTED PROJECT:-

/****************************PERSONAL DIARY
2006************************/
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdio.h>
#include<fstream.h>
#include<dos.h>
#include<stdlib.h>
int ucnt,u;
/*
'u' is current user's no.;
'ucnt' is total no. of users;
Both the variable are global because,
they are initialized every time the program starts.
*/
struct date dd;
struct time tt;
struct entrydate //Structure to store the date of the entry
{
int year,month,day;
};
struct entrytime
{
int hour,min,sec;
};
class diary //Class to store all the details of the entry
{
unsigned long char str[1000];
int u1;
public:
entrydate e;
entrytime tn;
int retu1()
{
return u1;
}
void in();
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 user //Class which contains the user information
{
char uname[10],pass[10];
int uno;
public:
int a;
int usercountinitialize();
void admit();
void checknewuser(class user);
void unoinitialize(int a)
{
uno=a;
}
void getnewuser()
{
clrscr();
cout<<"\nEnter desired username\t\t";
gets(uname);
cout<<"\nEnter desired password\t\t";
gets(pass);
a=ucnt+1;
unoinitialize(a);
}
void getuser()
{
clrscr();
cout<<"\t\t\tUSER IDENTIFICATION\n\n";
cout<<"\t\t\t--------case sensitive-------";
cout<<"\n\n\t\t\tUSERNAME\t:\t";
gets(uname);
cout<<"\n\t\t\tPASSWORD\t:\t";
gets(pass);
}
void verifyid();
void retuname()
{
cout<<uname;
}
int retuno()
{
return uno;
}
}jn;

void diary::in()
{
getdate(&dd);
di.e.year=dd.da_year;
di.e.month=dd.da_mon;
di.e.day=dd.da_day;
gettime(&tt);
di.tn.hour=tt.ti_hour;
di.tn.min=tt.ti_min;
di.tn.sec=tt.ti_sec;
di.u1=jn.retuno();
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);
if(u==1)
u1=1;
else
u1=u;
}

void user::checknewuser(user j1) /*Function which accepts new user's
username and password and
checks whether chosen username already exists or not*/
{ char chp,c='y';
int nuchk;
if(ucnt==0)
c='n';
while(c=='y')
{
ifstream fin("user.dat",ios::binary);
fin.seekg(0);
while(!fin.eof())
{
nuchk=0;
fin.read((char*)&jn,sizeof(user));
if((strcmpi(jn.uname,j1.uname))==0)
{
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')
{
j1.getnewuser();
checknewuser(j1);
break;
}
else
exit(0);
}
}
if(nuchk==0)
{
jn=j1;
c='n';
break;
}
fin.close();
}
}

int user::usercountinitialize() /*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(user));
if(jn.retuno()>big)
{
big=jn.retuno();
}
}
fin.close();
ucnt=big;
return 0;
}

void user::verifyid() /*Function which checks
whether entered id is valid or not*/
{
char c='y';
int c1;
while(c=='y')
{
user j2;
j2.getuser();
ifstream fin("user.dat",ios::binary);
fin.seekg(0);
while(!fin.eof())
{
c1=0;
fin.read((char*)&jn,sizeof(user));
if((strcmpi(j2.uname,jn.uname))==0&&(strcmpi(j2.pass,jn.pass))==0)
{
c='n';
c1=1;
cout<<"\nUSER VERIFIED";
getch();
u=jn.retuno();
break;
}
}
if(c1==0)
{
cout<<"\n\nINVALID USERNAME OR PASSWORD!!";
cout<<"\n\nWant to try again?";
cin>>c;
if(c=='n')
exit(0);
}
fin.close();
}
}

void user::admit() /*Function which writes the
new user's information onto the file*/
{
user j1;
ofstream fout("user.dat",ios::binary|ios::app);
if(!fout)
{
cout<<"\n\nError!!The file has been erased or renamed";
exit(0);
}
else
{
j1.getnewuser();
jn.checknewuser(j1);
fout.write((char*)&j1,sizeof(user));
}
fout.close();
}

void write() //Function to write the entry onto the file
{
char rep='y';
unsigned long char str[1000];
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
{
fin.seekg(0);
while(!fin.eof())
{
fin.read((char*)&di,sizeof(diary));
if(di.retu1()==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",ios::binary);
if(!fin)
cout<<"Error!!The file has been erased or renamed";
else
{
fin.seekg(0);
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.retu1()==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)
cout<<"Error!!The file has been deleted or renamed";
else
{
cout<<"\n\nEnter the date of the entry to be erased\n";
cout<<"\nYear\t";
cin>>yr;
cout<<"\nMonth\t";
cin>>mn;
cout<<"\nDay\t";
cin>>da;
fin.read((char*)&di,sizeof(diary));
if(di.retu1()!=u)
{
if(di.yr()!=yr&&di.mn()!=mn&&di.da()!=da)
{
fout.write((char*)&di,sizeof(diary));
}
}
remove ("diary.dat");
rename ("dia.dat","diary.dat");
cout<<"\n\nDeletion successful!";
}
fout.close();
fin.close();
}
void eraseall() //Function to erase the entire diary
{
ifstream fin("diary.dat",ios::binary);
ofstream fout("dia.dat",ios::binary|ios::app);
if(!fin)
cout<<"Error!!The file has been deleted or renamed";
else
{
fin.read((char*)&di,sizeof(diary));
if(di.retu1()!=u)
{
fout.write((char*)&di,sizeof(diary));
}
remove ("diary.dat");
rename ("dia.dat","diary.dat");
cout<<"\n\nDeletion successful!";
}
fout.close();
fin.close();
}

void main() //Main function
{
clrscr();
int ch,ch1,ch2,ch4,chk;
char ch3,a;
ifstream fin("user.dat",ios::binary);
if(!fin)
{
ucnt=0;
fin.close();
}
else
{ fin.close();

jn.usercountinitialize();
}
gotoxy(1,0);
cout<<"USER COUNT\t"<<ucnt;
gotoxy(25,15);
cout<<"CHOOSE FROM THE FOLLOWING FUNCTIONS:-";
gotoxy(25,17);
cout<<"1.USER SIGN UP";
gotoxy(25,19);
cout<<"2.USER LOGIN";
gotoxy(25,21);
cout<<"3.EXIT\t";
cin>>ch4 ;
switch(ch4)
{
case 1 :{
jn.admit();
chk=1;
jn.unoinitialize(ucnt+1);
break;
}
case 2 :{
ifstream fin("user.dat",ios::binary);
if(!fin)
{
cout<<"User file is deleted or renamed!";
exit(0);
break;
}
else
{if(ucnt>0)
{ jn.verifyid();
chk=1;
break;
}
else
{ cout<<"\n\nPlease sign up first";
getch();
main();
break;
}
}
}
case 3 :{
exit(0);
break;
}
default :{
cout<<"\nINVALID CHOICE!!";
getch();
ch3='y';
break;
}
}

if(chk==1)
{
do
{
clrscr();
cout<<"\t\t\t\tWELCOME ";
jn.retuname();
cout<<"\n\nChoose from the following actions\n\n1.ENTER
DIARY\n\n2.READ DIARY\n\n3.ERASE\n\n4.EXIT\n\n";
cin>>ch;
if(ch==1)
{
clrscr();
write();
ch3='y';
}
else if(ch==2)
{
clrscr();
cout<<"\n\n1.Read from specific date\n\n2.Show all\n\n";
cin>>ch1;
if(ch1==1)
{u=jn.retuno();
readon();
cout<<"\n\nWould you like to reach ";
jn.retuname();
cout<<"'s menu?(y/n)";
cin>>ch3;
}
if(ch1==2)
{
u=jn.retuno();
read();
cout<<"\n\nWould you like to reach ";
jn.retuname();
cout<<"'s menu?(y/n)";
cin>>ch3;
}
else
{
cout<<"\n\nINVALID CHOICE !!";
getch();
ch3='y';
}
}
else if(ch==3)
{
clrscr();
cout<<"\n\nChoose from the following erase functions\n\n1.Erase a
specific entry\n\n2.Erase the whole diary\n\n";
cin>>ch2;

if(ch2==1)
{ u=jn.retuno();
erase();
cout<<"\n\nWould you like to reach ";
jn.retuname();
cout<<"'s menu?(y/n)";
cin>>ch3;
}

else if(ch2==2)
{u=jn.retuno();
eraseall();
cout<<"\n\nWould you like to reach ";
jn.retuname();
cout<<"'s menu?(y/n)";
cin>>ch3;
}
else
{
cout<<"\n\nINVALID CHOICE!!";
getch();
ch3='y';
}
}
else if(ch==4)
{
exit(0);
}
else
{
cout<<"\n\nINVALID CHOICE!!";
getch();
ch3='y';
}
}while(ch3=='y'||ch3=='Y');
}
getch();
}


/**************END*****************/
 
Ad

Advertisements

T

Thomas J. Gritzan

rAgAv said:
I recommand you run this(^^^^^^) program. All you have to do is copy
and paste.No bugs.

I recommand not to. And it won't even compile.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdio.h>
#include<fstream.h>
#include<dos.h>
#include<stdlib.h>

This are all non-standard (non-existant) headers in C++.
Well, some are standard C headers.

[...]
gets(uname);

This C function is considered dangerous, since you can't control how much
characters were stored in the parameter. That means: A possible buffer
overflow.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top