In the menu(), in case 3 and 4, modification of object information is to be done. The modification is happening but afterwards the temporary file is not being removed and the temporary file is not being renamed to the main file - "userifo.dat". Therefore whenever the program is rerun the data is inaccessibly. Is the problem not what I understand it to be??
Would love any other off-topic suggestions for improvement.
Thanks
#include<iostream>
#include<string.h>
#include<fstream>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
using namespace std;
/**************************************************************************/
struct uinfo
{
char uname[50],pass[50],secq[300],seca[300];
float mbill;
int sccode[30],scno;
void newinfo()
{
system("cls");
cout<<"Enter Username : ";
cin.ignore();
cin.getline(uname,50);
cout<<"Enter Password : ";
cin.getline(pass,50);
cout<<"Enter a security question : ";
cin.getline(secq,300);
cout<<"Enter Answer to security answer : ";
cin.getline(seca,300);
mbill=0;
scno=0;
}
}userobj;
/**************************************************************************/
struct cinfo
{
char cname[30];
int ccode;
float price;
void display()
{
cout<<"Channel name : "<<cname<<"\nPrice : "<<price<<"\nCode : "<<ccode<<endl<<endl;
}
void input()
{
system("cls");
cout<<"Enter Name Of Channel : ";
cin.ignore();
cin.getline(cname,30);
cout<<"Enter price of subscription : ";
cin>>price;
cout<<"Enter The channel code : ";
cin>>ccode;
}
}chobj;
/**************************************************************************/
void adminmenu()
{
int cho=-1;
uinfo uobj;
while(cho!=0)
{
system("cls");
cout<<"\t\t\tWelcome to the Admin Menu\n\n"
<<"Enter what you want to do : \n"
<<"1. View the usernames and the"
<<" respective Monthly bill of all Users\n"
<<"2. Remove a user\n"
<<"3. Add a channel\n"
<<"4. Modify the price of a channel\n(It will change the Monthly Bill of all"
<<" users who have subscribed the channel)\n"
<<"0. Back to normal Menu\n";
cin>>cho;
switch(cho)
{
case 1 : {
ifstream file;
file.open("userifo.dat",ios::binary);
while(file.read((char*)&uobj,sizeof(uobj)))
{
cout<<"User : "<<uobj.uname<<"\nMonthly Bill : "<<uobj.mbill<<"\n\n";
}
getch();
break;
}
case 2 : {
char tempusern[50];
int flag=0;
cout<<"Enter the username of the user whose account is to be deleted : ";
cin.ignore();
cin.getline(tempusern,50);
ifstream file;
file.open("userifo.dat",ios::binary);
ofstream temp;
temp.open("temp.dat",ios::binary);
while(file.read((char*)&uobj,sizeof(uobj)))
{
if(strcmpi(tempusern,uobj.uname)!=0)
{
temp.write((char*)&uobj,sizeof(uobj));
}
else
flag=1;
}
file.close();
temp.close();
remove("userifo.dat");
rename("temp.dat","userifo.dat");
if(flag==0)
{
cout<<"No Such User....";
}
else
cout<<"User Deleted";
getch();
break;
}
case 3 : {
cinfo tempcobj;
tempcobj.input();
ofstream file;
file.open("channelinfo.dat",ios::binary|ios::app);
file.write((char*)&tempcobj,sizeof(tempcobj));
file.close();
break;
}
case 4 : {
system("cls");
ifstream file1;
file1.open("channelinfo.dat",ios::binary);
while(file1.read((char*)&chobj,sizeof(chobj)))
{
chobj.display();
}
file1.close();
cinfo tempcobj;
int tempcode,flag=0;
float cmbill,oprice;
cout<<"Enter the code of the channel that is to be updated : ";
cin>>tempcode;
ifstream file;
file.open("channelinfo.dat",ios::binary);
ofstream temp;
temp.open("temp.dat",ios::binary);
while(file.read((char*)&tempcobj,sizeof(tempcobj)))
{
oprice=tempcobj.price;
if(tempcobj.ccode!=tempcode)
{
temp.write((char*)&tempcobj,sizeof(tempcobj));
}
else
{
flag=1;
cout<<"Enter the new price of the channel : ";
cin>>tempcobj.price;
temp.write((char*)&tempcobj,sizeof(tempcobj));
fstream cprice;
cprice.open("userifo.dat",ios::binary|ios::app);
ofstream temp2;
temp2.open("temp2.dat",ios::binary|ios::app);
while(cprice.read((char*)&uobj,sizeof(uobj)))
{
for(int i=0;i<uobj.scno;++i)
{
if(tempcode==uobj.sccode[i])
uobj.mbill+=(oprice-tempcobj.price);
}
temp2.write((char*)&uobj,sizeof(uobj));
}
cprice.close();
temp2.close();
remove("userifo.dat");
rename("temp2.dat","userifo.dat");
}
}
if(flag==0)
{
cout<<"No Such Refferal Code !!! Plaes Try Again....";
getch();
}
file.close();
temp.close();
remove("channelinfo.dat");
rename("temp.dat","channelinfo.dat");
break;
}
case 0 : break;
default: cout<<"You've Entered a worng choice!!! Please Try Again....";
}
}
}
/************************************************************************/
void show_subchannel()
{
int i=0;
ifstream file;
for (i;i<userobj.scno;++i) {
file.open("channelinfo.dat",ios::binary);
while(file.read((char*)&chobj,sizeof(chobj)))
{
if(chobj.ccode==userobj.sccode[i])
chobj.display();
}
file.close();
}
getch();
}
/*************************************************************************/
void menu()
{
system("cls");
int choice=-1;
while(choice!=0)
{
system("cls");
cout<<"\t\t\t\tWelcome to the JC TV services menu\n\n\n"
<<"Enter your choice as to how you want to proceed -\n"
<<"1.View your Current Monthly Bill\n"
<<"2.View your Subscribed Channel list\n"
<<"3.Subscribe to a new Channel\n"
<<"4.Un-subscribe to a Channel\n"
<<"5.Enter into Administrator Mode\n"
<<"0.Log out and Exit\n";
cin>>choice;
switch(choice)
{
case 2:{
if(userobj.scno!=0)
{
show_subchannel();
}
else
cout<<"No Existing channels";
getch();
} break;
case 1:{
cout<<"Your Current Monthly Bill is : "<<userobj.mbill; getch();
}break;
case 3:{
ifstream file;
file.open("channelinfo.dat",ios::binary);
while(file.read((char*)&chobj,sizeof(chobj)))
{
chobj.display();
}
int tempcode;float addsum=0;
int flag=0;
for(;flag==0;)
{
cout<<"\nEnter the code of the channel you want to add : ";
cin>>tempcode;
for(int i=0;i<=userobj.scno;++i)
{
if(tempcode==userobj.sccode[i])
{
cout<<"Channel Already Subscribed.Please Enter another code";
break;
}
flag=1;
}
}
file.close();
file.open("channelinfo.dat",ios::binary);
int f=1;
while(file.read((char*)&chobj,sizeof(chobj)))
{
if(tempcode==chobj.ccode)
{
addsum=chobj.price;
userobj.mbill+=addsum;
userobj.sccode[userobj.scno]=tempcode;
userobj.scno++;
uinfo uobj;
ifstream umfile;
umfile.open("userifo.dat",ios::binary);
ofstream temp;
temp.open("temp.dat",ios::binary|ios::app);
while(umfile.read((char*)&uobj,sizeof(uobj)))
{
if(strcmpi(userobj.uname,uobj.uname)!=0)
{
temp.write((char*)&uobj,sizeof(uobj));
}
else
{
temp.write((char*)&userobj,sizeof(userobj));
}
}
file.close();
temp.close();
remove("userifo.dat");
rename("temp.dat","userifo.dat");
f=0;
}
}
if(f==1)
{
cout<<"No such refferal code...";getch();
}
}break;
case 4:{
show_subchannel();
int flag=1;
cout<<"Enter the code of the channel you want to un-subscribe to : ";
int tempcode;
cin>>tempcode;
for(int i=0;i<userobj.scno;++i)
{
if(tempcode==userobj.sccode[i])
{
flag=0;
for(int j=i;j<userobj.scno-1;++j)
userobj.sccode[j]=userobj.sccode[j+1];
userobj.scno--;
ifstream file;
file.open("channelinfo.dat",ios::binary);
float subsum;
while(file.read((char*)&chobj,sizeof(chobj)))
{
if(tempcode==chobj.ccode){
subsum=chobj.price;
userobj.mbill-=subsum;
userobj.scno--;
}
file.close();
uinfo uobj;
ifstream umfile;
umfile.open("userifo.dat",ios::binary);
ofstream temp;
temp.open("temp.dat",ios::binary|ios::app);
while(umfile.read((char*)&uobj,sizeof(uobj)))
{
if(strcmpi(userobj.uname,uobj.uname)!=0)
{
temp.write((char*)&uobj,sizeof(uobj));
}
else
{
temp.write((char*)&userobj,sizeof(userobj));
}
}
file.close();
temp.close();
remove("userifo.dat");
rename("temp.dat","userifo.dat");
}
}
if(flag==1)
{ cout<<"No such Referal Code"; getch(); }
}
}break;
case 5:{
char apass[50];
cout<<"Enter the Admin Special Password : ";
cin.ignore();
cin.getline(apass,50);
if (strcmpi(apass,"go for it")==0) {
adminmenu();
}
else
{ cout<<"Wrong Password...."; getch(); }
}break;
case 0: exit(0); break;
default:{
cout<<"Invaild Choice...";
}
}
}
}
/*************************************************************************/
void login()
{
system("cls");
char usernametemp[50],passwordtemp[50];
cout<<"Enter Username : ";
cin.ignore();
cin.getline(usernametemp,50);
cout<<"Enter Password : ";
cin.getline(passwordtemp,50);
int flag=0;
ifstream loginfile;
loginfile.open("userifo.dat",ios::binary);
while (loginfile.read((char*)&userobj,sizeof(userobj))) {
if (strcmpi(userobj.uname,usernametemp)==0) {
if (strcmpi(userobj.pass,passwordtemp)==0) {
flag=1;
menu();
}
else {
cout<<"Wrong password!!!";
cout<<"\nAnswer the following Question : \n"<<userobj.secq<<endl;
char secqa[300];
cin.getline(secqa,300);
if (strcmpi(secqa,userobj.seca)==0) {
flag=1;
menu();
}
else{
cout<<"Wrong Answer!!!! Try Again.....";
getch();
login();
}
}
}
}
if (flag==0) {
{std::cout << "No Such Username Exists!!! Try again...";
getch();
login();
}
}
loginfile.close();
}
/*************************************************************************/
void newuser()
{
int flag=1;
uinfo tempobj,tempobj2;
while(1)
{
tempobj.newinfo() ;
ifstream nufile;
nufile.open("userifo.dat",ios::binary|ios::in);
while(nufile.read((char*)&tempobj2,sizeof(tempobj2)))
{
if(strcmpi(tempobj.uname,tempobj2.uname)==0)
{
flag=0;
cout<<"Username already exists!!! Try again...";
getch();
nufile.close();
break;
}
else
flag=1;
}
if(flag==1)
{
ofstream nu1file;
nu1file.open("userifo.dat",ios::binary|ios::app);
nu1file.write((char*)&tempobj,sizeof(tempobj));
cout<<"User Created";
userobj=tempobj;
getch();
nu1file.close();
menu();
break;
}
}
}
/*************************************************************************/
int main()
{
char ch;
cout<<"\t\t\t WELCOME\n\n"
<<"Are you a new User (Y/N) : ";
cin>>ch;
if(toupper(ch)=='Y')
{
int k=1;
while(k=1)
{
newuser();
}
}
else
{
login();
}
return 0;
}
/*************************************************************************/
Aucun commentaire:
Enregistrer un commentaire