Code:
#include<iostream.h>#include<stdlib.h>
#include<stdio.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
struct date
{
int date;
int monthno;
int year;
char month[15];
};
class Account
{
char password[30];
char sec_ans[30];
public:
char status[150];
char email[40];
char name [30];
char sec_ques[60];
date b_day;
void getdata(); //To be used when creating new account
void open();
int comp(char id[],char pass[]);
void change(); // To change account info
void Upd_stat();
void getbdate();
void change_pass();
void change_mail();
}acc,temp;
void Account :: Upd_stat()
{
clrscr();
cout<<"Enter status : ";
gets(status);
cout<<"\nStatus updated successfully!";
}
void Account :: change_mail()
{
clrscr();
int exist=0;
char id[40];
cout<<"Enter new email address : ";
gets(id);
ifstream fin("facebuk.dat",ios::in);
fin.seekg(0);
while(!fin.eof())
{
fin.read((char*)&temp,sizeof(temp));
if(strcmp(id,temp.email)==0)
{
exist=1;
if(strcmp(id,email)==0)
cout<<"That is your currecnt email address!";
else
cout<<"Account with the email address already exists";
}
}
if(!exist)
{
strcpy(email,id);
cout<<"\nE-mail address changed Successfully!";
}
fin.close();
}
void Account :: change_pass()
{
char pass[30],conf[30];
clrscr();
cout<<"Enter your current password : ";
gets(pass);
if(strcmp(pass,password))
{
cout<<"Wrong password!";
return;
}
do
{
cout<<"Enter new password : ";
gets(pass);
cout<<"Confirm new password : ";
gets(conf);
if(strcmp(pass,conf))
cout<<"Passwords do not match! Try agian!\n";
}while(strcmp(pass,conf));
strcpy(password,pass);
cout<<"\nPassword canged successfully!";
}
void Account :: getbdate()
{
int wrong;
cout<<"Enter Date of Birth :\n";
do
{
wrong=0;
cout<<"Enter Date(1-31) : ";
cin>>b_day.date;
if(b_day.date<1 || b_day.date>31)
{
cout<<"Invalid Date!\n";
wrong=1;
}
}while(wrong);
do
{
wrong=0;
cout<<"Enter month(1-12) : ";
cin>>b_day.monthno;
switch(b_day.monthno)
{
case 1 : strcpy(b_day.month,"January");
break;
case 2 : strcpy(b_day.month,"February");
break;
case 3 : strcpy(b_day.month,"March");
break;
case 4 : strcpy(b_day.month,"April");
break;
case 5 : strcpy(b_day.month,"May");
break;
case 6 : strcpy(b_day.month,"June");
break;
case 7 : strcpy(b_day.month,"July");
break;
case 8 : strcpy(b_day.month,"August");
break;
case 9 : strcpy(b_day.month,"September");
break;
case 10 : strcpy(b_day.month,"October");
break;
case 11 : strcpy(b_day.month,"November");
break;
case 12 : strcpy(b_day.month,"December");
break;
default : cout<<"Invalid month!\n";
wrong=1;
}
}while(wrong);
cout<<"Enter year : ";
cin>>b_day.year;
}
void Account :: change()
{
int ch,wrong=0;
do
{
clrscr();
cout<<"What would you like to change?\n";
if(wrong)
cout<<"\t\t\t\tINVALID CHOICE!\n";
wrong=0;
cout<<"- Change email address(Press 1)\n- Change Name(Press 2)\n- Change Password(Press 3)\n- Chnage Date of Birth(Press 4)\n";
cin>>ch;
switch(ch)
{
case 1 : change_mail();
break;
case 2 : cout<<"Enter name : ";
gets(name);
cout<<"\nName changed successfully!";
break;
case 3 : change_pass();
break;
case 4 : getbdate();
cout<<"\nDate of birth changed successfully!";
break;
default : wrong=1;
}
}while(wrong);
getch();
}
void Account :: open()
{
int ch,logout;
do
{
logout=0;
clrscr();
cout<<"\t\t\t\tWELCOME ";
puts(name);
cout<<"\n\nWhat would you like to do today?\n\n- Update Status(Press 1)\n- Change Account info(Press 2)\n- Logout(Press 3)\n";
cin>>ch;
switch(ch)
{
case 2 : change();
break;
case 3 : logout=1;
break;
case 1 : Upd_stat();
break;
default : cout<<"\n\n\t\t\tINVALID CHOICE. TRY AGAIN!";
}
}while(!logout);
}
int Account :: comp(char id[40],char pass[30])
{
if(strcmp(id,email)==0 && strcmp(pass,password)==0)
return 1;
return 0;
}
void Account :: getdata()
{
clrscr();
cout<<"\t\t\t\tCREATE AN ACCOUNT\\n\n";
cout<<"Enter Name : ";
gets(name);
cout<<"Enter Email id : ";
gets(email);
getbdate();
cout<<"Enter password : ";
gets(password);
cout<<"Enter a security question(In case you forget your password) : ";
gets(sec_ques);
cout<<"Enter the answer to your security question : ";
gets(sec_ans);
}
void Profile(int pos)
{
ifstream fi("facebuk.dat",ios::in);
fi.seekg(pos);
fi.read((char*)&acc,sizeof(acc));
fi.close();
acc.open();
ofstream fo("facebuk.dat",ios::out);
fo.seekp(pos);
fo.write((char*)&acc,sizeof(acc));
fo.close();
//ifstream fi("facebuk.dat",ios::in);
}
void login()
{
char id[40],pass[30],tr;
int check,again,wrong;
int pos;
do
{
again=0;
clrscr();
cout<<"\t\t\t\t\tLOGIN\n\n";
cout<<"Enter Email id : ";
gets(id);
cout<<"\nEnter password : ";
gets(pass);
ifstream fi("facebuk.dat",ios::in);
fi.seekg(0);
while(fi)
{
pos=fi.tellg();
fi.read((char*)&acc,sizeof(acc));
check=acc.comp(id,pass);
if(check)
break;
}
fi.close();
if(!check)
{
cout<<"Incorrect Email and/or password.Try again?(y/n) ";
do
{
wrong=0;
cin>>tr;
switch(tr)
{
case 'Y' :
case 'y' : again=1;
break;
case 'N' :
case 'n' : again=0;
break;
default : cout<<"Invalid . Try again.";
wrong=1;
}
}while(wrong);
}
}while(again);
if(check)
Profile(pos);
}
void create()
{
acc.getdata();
ofstream fo("facebuk.dat",ios::out | ios::app);
fo.write((char*)&acc,sizeof(acc));
fo.close();
}
void forgot()
{
cout<<"\nSorry, program under construction!";
getch();
}
void main()
{
int ch,exit=1,wrong=0;
do
{
clrscr();
cout<<"\t\t\t\tWELCOME TO FACEBUK\n\n";
if(wrong)
{
cout<<"\t\t\t INVALID CHOICE. TRY AGIAN.\n\n";
wrong=0;
}
cout<<"- Login (Press 1)\n- Create a new account (Press 2)\n- Forgot Password? (Press 3)\n- Exit (Press 4)";
cin>>ch;
switch(ch)
{
case 1 : login();
break;
case 2 : create();
break;
case 3 : forgot();
break;
case 4 : exit=0;
break;
default : wrong=1;
}
}while(exit);
}