BGI error.Graphics executes well but c++ program hangs

I only want graphics for a part of the program.that is why i am initializing graphics and closing it once the job is over.The graphics part executes well, but when it comes back to selectdb(),the graphics is now closed and the normal program executes.Now,in one of the member functions,display_tbl(),i am not able to read a file and display it.What could be the problem?

I get "BGI Error:Graphics not initialized.use initgraph.....". but the graphics part executes well for me.

#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <fstream.h>
#include <conio.h>
#include <iomanip.h>
#include <graphics.h>

const int c=10,r=10; //Maximum no. of rows and columns
const int len=20;


char password[20],dbname[20];


class DBMS
{

public:
int integer[r][c];
int fld_type[c],row,col;
char charac[r][c];
char str[r][c][len];
char field_name[c][20];
char tbl_name[20];

fstream f;

void createdb();
void selectdb();
void disp_tbl();
void add_col();
void add_row();
void save_tbl();
};


void DBMS::disp_tbl() //to display whole table
{
char t_name[20];
cout<<"\nEnter the table name: ";
cin.getline(t_name,20);
f.open(t_name,ios::binary|ios::in);
while(f.read((char*)&this,sizeof(DBMS)))
{

for ( int j=0;j<col;++j)
{
cout<<setw(10)<<field_name[j];
}
cout<<setw(5)<<endl<<"-------------------------------------------------------------";

for(int in=0;in<row;++in)
{
cout<<endl;

for(int i=0;i<col;++i)
{

if (fld_type[i]==1)
{
if(integer[in][i]!=-200) //-200 is chosen as the illegal value
{
cout<<setw(10)<<integer[in][i];
}
}

else if (fld_type[i]==2)
{
if(charac[in][i]!='\0')
{
cout<<setw(10)<<" "<<charac[in][i];
}
else cout<<setw(10)<<" ";
}

else if (fld_type[i]==3)
{
if(strcmp(str[in][i],"a")!=0) //'a' is chosen as the illegal string.
{
cout<<setw(10);
cout<<str[in][i];
}
else cout<<setw(10)<<" ";
}

}

}
}

cin.get();
cout<<endl<<"press <enter>";cin.get();
f.close();
}

void DBMS::add_col() //to add a column
{

char reply;
int b;
do
{
b=col;
if(col+1 !=c)
{

cout<<"\nEnter name of the field:"<<endl;
cin.getline(field_name[col],20);
cout<<"\nChoose field type:"<<endl
<<"\n1.Int"<<endl
<<"\n2.Char"<<endl
<<"\n3.String"<<endl;
cin>>fld_type[col];cin.get();
col++;
}
else cout<<"\n\nyou have crossed the column limit....!!";
cin.get();
cout<<"\nwant another column?[y/n]";
cin>>reply;cin.get();
}while(reply=='y'|| reply=='Y');
cout<<endl<<"\nNo of columns: "<<col
<<endl<<"\nNo of rows: "<<row;


for(int k=0;k<row;++k)
{

for(int a=b;a<col;++a)
{
if(fld_type[a]==1)
{
cout<<endl<<"\nEnter the value for: "<<field_name[a]<<endl;
cin>>integer[k][a];cin.get();

}
else if(fld_type[a]==2)
{
cout<<"\nEnter the character for: "<<field_name[a]<<endl;
cin>>charac[k][a];cin.get();

}
else if(fld_type[a]==3)
{
cout<<"\nEnter the string for : "<<field_name[a]<<endl;
cin.getline(str[k][a],len);
cin.get();
}
else cout<<"\n\ntype mismatch";

}

}

}
void DBMS::add_row() //to add a row
{

char reply;
int k=0;
do
{
row++;
k=row-1;
for(int a=0;a<col;++a)
{

if(fld_type[a]==1)
{
cout<<endl<<"\nEnter the value for: "<<field_name[a]<<endl;
cin>>integer[k][a];cin.get();

}
else if(fld_type[a]==2)
{
cout<<"\nEnter the character for: "<<field_name[a]<<endl;
cin>>charac[k][a];cin.get();

}
else if(fld_type[a]==3)
{
cout<<"\nEnter the string for : "<<field_name[a]<<endl;
cin.getline(str[k][a],len);
cin.get();
}
else cout<<"\n\ntype mismatch";


}
cout<<"\nwant another row??[y/n]";
cin>>reply;cin.get();
}while(reply=='y'||reply=='Y');


}
void DBMS::save_tbl() //to save the table.
{
if(!f)
{
cout<<"\nTable does not exist...";
cin.get();
exit(2);
}
f.open(tbl_name,ios::binary|ios::out);
f.write((char*)&this,sizeof(DBMS));
f.close();
}
void DBMS::selectdb() //operations on table
{
fstream file;
clrscr();
char name[20],pass[20];
cout<<"\nEnter the name of the database: ";
cin.getline(name,20);
file.open(name,ios::binary|ios::in);
if(!file)
{
cout<<"\nthe database does not exist!!";
cin.get();
exit(4);
}
else cout<<"\nEnter the password to access the databse: ";
cin.getline(pass,20);
cin.get();
file.read((char*)&password,sizeof(password));

if(strcmp(pass,password)==0)
{
cout<<"\nAccess to database "<<name<<" granted...";
cout<<"\npress <enter> to continue...";
cin.get();

file.close();

int option;
char reply;
do{
clrscr();
cout<<"\n===============================OPERATIONS================================"<<endl
<<"\n1.Create a table"<<endl
<<"\n2.Display table"<<endl
<<"\n3.Search Information"<<endl
<<"\n4.Add a column"<<endl
<<"\n5.Add a row"<<endl
<<"\n6.Delete a column"<<endl
<<"\n7.Delete a row"<<endl
<<"\n8.Modify data"<<endl
<<"\n9.Save table"<<endl
<<"\n10.Exit App"<<endl
<<"==========================================================================="<<endl;
cin>>option;cin.get();
switch(option)
{
case 1:createtbl();
break;
case 2:disp_tbl();
break;
case 3: srch_info();
break;
case 4: add_col();
break;
case 5: add_row();
break;
case 9:save_tbl();
break;
case 10: exit(3);
break;
default:cout<<"\nInvalid number"<<endl;
}
cout<<"\nAnother operation?[y/n]"<<endl;
cin>>reply;cin.get();
}while(reply=='y'||reply=='Y');
}
else cout<<"\npasswords dont match....";
}

int main()
{
clrscr();
DBMS d;
int choice;
char wish;
do
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"D:\\TC\\BGI");
setbkcolor(LIGHTGRAY);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,3);
setcolor(BLUE);
outtextxy(20,1," PERSONAL DATA MANAGER ");
settextstyle(TRIPLEX_FONT,HORIZ_DIR,1);
setcolor(MAGENTA);

outtextxy(210,50,"MAIN MENU");
outtextxy(200,70,"1.Create a database");
outtextxy(200,90,"2.Select a database");
outtextxy(200,110,"3.Exit");
outtextxy(20,150,"Enter an option");
cin>>choice;cin.get();

closegraph();
switch(choice)
{
case 1:d.createdb();
break;
case 2:d.selectdb();
break;
case 3:cout<<"\n\nQuitting application......";
exit(1);
default : cout<<"\nInvalid number";
}

cout<<"\nDo you want to perform another operation??[y/n]";
cin>>wish;cin.get();
}while(wish=='y'||wish=='Y');

return 0;
}
Topic archived. No new replies allowed.