A part of the output is wrong.....

I just tried a new program with a fixed no. of columns.I'm getting a few
This is a prog to create a DBMS with a fixed max no of columns.

problems with output:
when i want the 1st column to be int type and the 2nd column to be of type char,the 1st 3 rows of the 1st column alone is showing some randon junk value.The rest of the output is perfect
I think there should be some problem with the allocation of these 3 places.....
But when i hav 2 columns of int or 2 columns of char,the output is perfect....



here is the code :

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

const int c=10;
const int len=15;

struct table
{
char field_name[c][20];
int fld_type[c];
char tbl_name[20];
int col,row;
};

struct database
{
char username[20],password[10];
table tbl;
}db;

class DBMS
{

public:
int integer[][c];
char charac[][c];
char str[][c][len];

fstream f;
void createdb();
void selectdb();
void disp_tbl();
void disp_col();
};

void DBMS::createdb()
{

db.tbl.col=0;
char reply;
cout<<"\nEnter username:"<<endl;
cin.getline(db.username,20);cin.get();
cout<<"\nEnter the password upto 8 characters,containing alphabets and numbers: "<<endl;
cin.getline(db.password,10);cin.get();
clrscr();
cout<<"\nEnter the name of the table:"<<endl;
cin.getline(db.tbl.tbl_name,20);
fstream f(db.tbl.tbl_name,ios::binary|ios::ate|ios::in);
cout<<"\nEnter the total number of tuples you want :"<<endl;
cin>>db.tbl.row;cin.get();
f.write((char*)&(db),sizeof(db));

do
{
if(db.tbl.col!=c)
{
cout<<"\nEnter name of the field:"<<endl;
cin.getline(db.tbl.field_name[db.tbl.col],20);
f.write((char*)&(db.tbl),sizeof(db.tbl));
cout<<"\nChoose field type:"<<endl
<<"\n1.Int"<<endl
<<"\n2.Char"<<endl
<<"\n3.String"<<endl;
cin>>db.tbl.fld_type[db.tbl.col];cin.get();
db.tbl.col+=1;
f.write((char*)&(db.tbl),sizeof(db.tbl));
cout<<"\nWant another column?"<<endl;
cin>>reply;cin.get();
}
else cout<<"\n\nyou have crossed the column limit..!!";

}while(reply=='y'||reply=='Y');

cout<<endl<<"\nNo of columns: "<<db.tbl.col
<<endl<<"\nNo of rows: "<<db.tbl.row;



for(int k=0;k<db.tbl.row;++k)
{
for(int a=0;a<db.tbl.col;++a)
{

if(db.tbl.fld_type[a]==1)
{

cout<<endl<<"\nEnter the value:"<<endl;
cin>>integer[k][a];cin.get();
cout<<endl<<integer[k][a];
cout<<endl<<"updating "<<k<<" "<<a;

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

}
else if(db.tbl.fld_type[a]==3)
{
cout<<"\nEnter the string:"<<endl;
for(int s=0;s<len;++s)
{

cin>>str[k][a][s];

}
cout<<endl<<"updating "<<k<<" "<<a;

}
f.write((char*)&(db.tbl),sizeof(db.tbl));

}

}



cin.get();

clrscr();
}
void DBMS::disp_tbl()
{
fstream f(db.tbl.tbl_name,ios::binary|ios::in|ios::ate);
if(!f)
{
cerr<<endl<<db.tbl.tbl_name<<"could not be opened for displaying...press <enter>";
cin.get();
exit(1);
}

while(!f.eof())
{
cout<<"\nno of rows : "<<db.tbl.row<<" and columns : "<<db.tbl.col<<endl;
for ( int j=0;j<db.tbl.col;++j)
{
cout<<setw(20)<<db.tbl.field_name[j];
f.read((char*)&db.tbl,sizeof(db.tbl));
}


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

for(int i=0;i<db.tbl.col;++i)
{
cout<<setw(20);
if (db.tbl.fld_type[i]==1)
{
if(integer[in][i]!=-200) //-200 is chosen as illegal value
{
cout<<setw(20)<<integer[in][i];
}
}

else if (db.tbl.fld_type[i]==2)
{
if(charac[in][i]!='-') //'-' is chosen as illegal.
{
cout<<setw(20)<<charac[in][i];
}
}


else if (db.tbl.fld_type[i]==3)
{
if(str[in][i][len]!='\0') //null string represents undesirable display.
{
cout<<setw(20);
for(int s=0;s<len;++s)
{
if(str[in][i][s]!='.')
cout<<str[in][i][s];
}
}
}
f.read((char*)&db.tbl,sizeof(db.tbl));
}

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

}


int main()
{
clrscr();
DBMS d;
int choice;
char loop;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl
<<"\n\n DATABASE MANAGEMENT SYSTEM "<<endl
<<"\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;

do
{

cout<<"\n\n================================MAIN MENU================================="<<endl
<<"\n1.Create a database"<<endl
<<"\n2.Select a database"<<endl
<<"\n3.Exit"<<endl;
cout<<"\nEnter an option:"<<endl;
cin>>choice;cin.get();
switch(choice)
{
case 1:d.createdb();
break;
case 2:d.selectdb();
break;
case 3:exit(1);
default : cout<<"\nInvalid number"<<endl;
}
cout<<"\nAnother operation?"<<endl;
cin>>loop;cin.get();
}while(loop=='y'||loop=='Y');
return 0;

}
Topic archived. No new replies allowed.