Hello. The problem with the following code is that when I read from a file after restarting the program and then try to display the contents, it goes into an endless loop. I hope that someone can point out the logical error hiding there.
#include <iostream>
#include <windows.h>
#include <fstream>
#include "link.h";
;using namespace std;
class qu
{
qu * next,*front,*rear;
int d;
int age;
string name;
public:
qu() {
next=0;
front=rear=0;
}
~qu() { }
void push(int n, int ag, string nam)
{
qu *newptr=new qu;
newptr->d=n;
newptr->age=ag;
newptr->name=nam;
if(front==0)
{
front=rear=newptr;
}
else {
newptr->next=0;
rear->next=newptr;
rear=newptr;
}
}
int pop()
{
if(front==0)
{
return -1;
}
else
{
qu *newptr=this->front;
front=front->next;
int n=newptr->d;
delete newptr;
return n;
}
}
void disp()
{
cout<<"|---------------------------------------|"<<endl;
cout<<"| Student Data |"<<endl;
cout<<"|=======================================|"<<endl;
cout<<"|Admission Number: "<<d<<" |"<<endl;
cout<<"|Student's Name: "<<name<<" |"<<endl;
cout<<"|Student's Age: "<<age<<" |"<<endl;
cout<<"|=======================================|"<<endl;
}
void traverse()
{
system("cls");
qu * newptr=this->front;
cout<<"Student Details: "<<'\n';
if(newptr!=0) {
while(newptr!=0)
{
newptr->disp();
newptr=newptr->next;
}
delete newptr;
}
else cout<<"qu Empty!\n";
cont();
}
void SaveDat();
void ReadDat();
};
void qu::SaveDat()
{
fstream Dat;
system("cls");
qu * newptr=this->front;
Dat.open("datat.txt",ios::out);
if(Dat) {
cout<<"Saving to file...\n\n";
if(newptr!=0) {
while(newptr!=0)
{
Dat.write((char *) newptr, sizeof(qu));
newptr=newptr->next;
}
delete newptr;
Dat.close();
cout<<"Saved!\n";
}
else cout<<"Qu empty! Nothing to save!\n";
}
else cout<<"Error! Could not open file for write!\n";
cont();
}
void qu::ReadDat()
{
fstream Dat;
system("cls");
qu * newptr=new qu;
Dat.open("datat.txt",ios::in|ios::beg);
if(Dat.eof()) { cout<<"No Data in file!\n\n"; }
else {
while(!Dat.eof()) {
Dat.read((char *)newptr, sizeof(qu));
if(front==0)
{
front=rear=newptr;
}
else
{
newptr->next=0;
rear->next=newptr;
rear=newptr;
}
}
Dat.close();
cout<<"qu Read!\n";
}
cont();
}
void PrntBox();
void enterInQu(qu * mqu)
{
int n,ag;
string nam;
char ch;
do {
cout<<"Enter D number of student:\t";
cin>>n;
cout<<"Enter age of student:\t";
cin>>ag;
cout<<"Enter name of student:\t";
cin.ignore(1,'\n');
getline(cin,nam);
mqu->push(n,ag,nam);
cout<<"Enter more elements?(y/n)\n";
cin>>ch;
}while(ch=='y'||ch=='Y');
}
void deleteFrmQu(qu *mqu)
{
int n=1; //Identifier to show qu empty or full
char ch='y';
while(ch=='y'&&n!=-1)
{
n=mqu->pop();
if(n!=-1)
{
cout<<"Student record with D-number "<<n<<" deleted!\n";
cout<<"Want to delete more students?(y/n)\n";
cin>>ch;
}
else{
cout<<"Qu is empty! Nothing to remove!\n";
}
}
}
void menu()
{
qu * MyQu=new qu;
int choice;
char ch;
while(1) {
system("cls");
system("COLOR 2");
PrntBox();
cin>>choice;
switch(choice)
{
case 1: enterInQu(MyQu);
break;
case 2: MyQu->traverse();
break;
case 3: deleteFrmQu(MyQu);
break;
case 4: MyQu->ReadDat();
break;
case 5:
MyQu->SaveDat();
break;
case 6: cout<<"Exiting Program...\n Press any Key to continue\n";
cin.ignore(1,'\n');
cin.get();
exit(0);
}
}
}
int main()
{
cout<<"Program to make a queue database...\n";
menu();
}
void PrntBox()
{
cout<<"1.Enter Data\n2.View Data\n3.Delete Data\n4.Read From File\n5.Save to file\n6.Exit\n\t";
}