i am doing a project on student information system using file handling...i made my own project but it didnt work....i read many books but actually the main problem is that i lack logic.....i have found from the net this project but the output on the notepad file is giving unusual symbols along with required data....plz help with output as quick as possible.........(it works in turbo c++ 3.0 as i have used gotoxy and clrscr.....and output notepad file will be saved in C drive
enum bool{false,true};
struct Data
{
int id;
char first_name[20];
char last_name[20];
char address[40];
char city[20];
char email_id[20];
char rollno[20];
char phone[15];
};
class Student
{
public:
Student();
void AddRecord();
void DelRecord();
void CreateFile();
void ViewRecord();
private:
void ReadFromFile(Data[]);
//precondition:array has been filled by the value either from user or constructor
//postcondition:array has been updated according to data in file student.dat
bool SearchEmptyRecord(Data[],int,int&);
//precondition:array has been filled by the value either from user or constructor
//postcondition:return the condition whether the empty record is found or not, and its location
void WriteToFile(Data[]);
//precondition:array has been updated by user
//postcondition:array is written on the file student.dat
bool Search(Data[],int,int,int&);
//precondition:array has been filled by the value either from user or constructor
//postcondition:return the condition whether the searched record is found or not, and its location
void ShowRecord(Data[],int);
//precondition:array has been filled by the value either from user or constructor
//postcondition:display on the screen the record about student information
Data record[11];
};
# include<iomanip.h>
# include<string.h>
# include<fstream.h>
# include<stdlib.h>
# include<conio.h>
Student::Student()//constructor
{
for(int i=0;i<11;i++)
{
record[i].id=0;
strcpy(record[i].first_name," ");
strcpy(record[i].last_name," ");
strcpy(record[i].address," ");
strcpy(record[i].city," ");
strcpy(record[i].email_id," ");
strcpy(record[i].rollno," ");
strcpy(record[i].phone," ");
}
}
void Student::CreateFile() //this class implementation is only used when the first
{ //time i run the program to create file
ofstream fout("C:\student.txt",ios::out);
for(int i=0;i<11;i++)
{
fout<<"|"<<record[i].id<<"|";
fout<<record[i].first_name<<"|";
fout<<record[i].last_name<<"|";
fout<<record[i].address<<"|";
fout<<record[i].city<<"|";
fout<<record[i].email_id<<"|";
fout<<record[i].rollno<<"|";
fout<<record[i].phone<<"\n";
}
fout.close();
}
void Student::AddRecord() //to add record to the blank record
{
bool found;
int confirm;
int location;
int target;
char ans;
ReadFromFile(record);
do
{
clrscr();
found=SearchEmptyRecord(record,11,location); //to find empty record
if (!found)
{
cout<<"You have reached maximum capacity\n";
}
else
{
do
{
clrscr();
gotoxy(14,10);cout<<"enter student id\t\t: ";
cin>>target;
confirm=Search(record,11,target,location);
if(confirm)
{
gotoxy(14,11);cout<<"you have duplicated id, please enter another id !!";
gotoxy(14,12);cout<<"please press y and press enter !!";
cin>>ans;
}
}while(confirm&&((ans=='y')||(ans=='Y')));
record[location].id=target;
cin.ignore();
gotoxy(14,11);cout<<"enter student first name\t: ";
cin.get(record[location].first_name,20);
cin.ignore();
gotoxy(14,12);cout<<"enter student last name\t: ";
cin.get(record[location].last_name,20);
cin.ignore();
gotoxy(14,13);cout<<"enter student address\t: ";
cin.get(record[location].address,40);
cin.ignore();
gotoxy(14,14);cout<<"enter city\t\t\t: ";
cin.get(record[location].city,20);
cin.ignore();
gotoxy(14,15);cout<<"enter email_id\t\t: ";
cin.get(record[location].email_id,20);
cin.ignore();
gotoxy(14,16);cout<<"enter rollno \t\t: ";
cin.get(record[location].rollno,20);
cin.ignore();
gotoxy(14,17);cout<<"enter phone\t\t: ";
cin.get(record[location].phone,15);
WriteToFile(record);
}
gotoxy(14,18);cout<<"do you want to add record again (y/n) : ";
cin>>ans;
}while(ans=='y'||ans=='Y');
}
void Student::ReadFromFile(Data a[])//to read the data from the file and assign them to
{ //variable
ifstream fin("C:\student.txt",ios::in);
for(int i=0;(!fin.eof()&&i<11);i++)
{
fin.ignore();
fin>>a[i].id;
fin.ignore();
fin.get(a[i].first_name,20,'|');
fin.ignore();
fin.get(a[i].last_name,20,'|');
fin.ignore();
fin.get(a[i].address,40,'|');
fin.ignore();
fin.get(a[i].city,20,'|');
fin.ignore();
fin.get(a[i].email_id,20,'|');
fin.ignore();
fin.get(a[i].rollno,20,'|');
fin.ignore();
fin.get(a[i].phone,15,'|');
}
fin.close();
}
bool Student::SearchEmptyRecord(Data a[],int size,int& location)
{
int i=0;
bool found=false;
while((i<size)&&(!found))
{
if(a[i].id==0)//if id# equal to zero it means the record is empty
{
found=true;
location=i;
}
else
i++;
}
return found;
}
void Student::WriteToFile(Data a[])//to write the data hold in variable to the file
{
ofstream fout;
fout.open("C:\student.txt",ios::out);
for(int i=0;i<11;i++)
{
fout<<"|"<<a[i].id<<"|";
fout<<a[i].first_name<<"|";
fout<<a[i].last_name<<"|";
fout<<a[i].address<<"|";
fout<<a[i].city<<"|";
fout<<a[i].email_id<<"|";
fout<<a[i].rollno<<"|";
fout<<a[i].phone<<"\n";
}
fout.close();
}
void Student::ViewRecord()//to view certain record with using student id as primary key
{
int key,location;
bool found;
ReadFromFile(record);
gotoxy(14,9);cout<<"Enter student # id number that you want to see : ";
cin>>key;
found=Search(record,11,key,location);
if (!found)
{
gotoxy(14,10);cout<<"Record not found\n";
}
else
ShowRecord(record,location);
}
bool Student::Search(Data a[],int size,int target,int& location)
{ //to find certain record in array with using linear search
int i=0;
bool found=false;
while((i<size)&&(!found))
{
if (a[i].id==target)
{
found=true;
location=i;
}
else
i++;
}
return (found);
}
void Student::DelRecord()
{
char ans;
int key,location,j;
int i=0;
bool found;
ReadFromFile(record);
do
{
gotoxy(14,9);cout<<"Enter student id # that you want to delete : ";
cin>>key;
clrscr();
found=Search(record,11,key,location);
if (!found)
{
gotoxy(14,10);cout<<"Record Not Found !!!\n";
gotoxy(14,11);cout<<"These are the list of valid ID and Name\n";
gotoxy(14,12);cout<<"-----------------------------------------\n";
gotoxy(14,13);cout<<"ID# FIRST_NAME LAST_NAME\n";
gotoxy(14,14);cout<<"-----------------------------------------\n";
while(i<11)
{
if(record[i].id!=0)
{
gotoxy(14,15+i);cout<<record[i].id;
gotoxy(24,15+i);cout<<record[i].first_name;
gotoxy(42,15+i);cout<<record[i].last_name<<"\n";
j=i;
}
i++;
}
gotoxy(14,16+j);cout<<"-----------------------------------------\n";
gotoxy(14,17+j);cout<<"Please re-enter the valid id !!";
}
else
{
found=true;
ShowRecord(record,location);
gotoxy(14,18);cout<<"Are you sure you want to delete this record(y/n) : ";
cin>>ans;
if (ans=='y'||ans=='Y')
{
record[location].id=0;
strcpy(record[location].first_name," ");
strcpy(record[location].last_name," ");
strcpy(record[location].address," ");
strcpy(record[location].city," ");
strcpy(record[location].email_id," ");
strcpy(record[location].rollno," ");
strcpy(record[location].phone," ");
WriteToFile(record);
}
}
}while(!found);
}
void Student::ShowRecord(Data a[],int location)
{
gotoxy(14,10);cout<<"Student id\t\t: "<<a[location].id<<endl;
gotoxy(14,11);cout<<"Student first name\t: "<<a[location].first_name<<endl;
gotoxy(14,12);cout<<"Student last name\t: "<<a[location].last_name<<endl;
gotoxy(14,13);cout<<"Student address\t: "<<a[location].address<<endl;
gotoxy(14,14);cout<<"Student city\t: "<<a[location].city<<endl;
gotoxy(14,15);cout<<"Student email_id\t: "<<a[location].email_id<<endl;
gotoxy(14,16);cout<<"Student rollno\t: "<<a[location].rollno<<endl;
gotoxy(14,17);cout<<"Student phone\t: "<<a[location].phone<<endl;
}
char main_menu();
int main()
{
char ans;
char choice;
Student StudentData;
//StudentData.CreateFile(); //used to create file at the first time i run the program
do
{
clrscr();
choice=main_menu();
switch (choice)
{
case '1':clrscr();
StudentData.ViewRecord();
break;
case '2':clrscr();
StudentData.AddRecord();
break;
case '3':clrscr();
StudentData.DelRecord();
break;
default :cout<<"Invalid Option";//error message #1
}
gotoxy(14,22);cout<<"Press 1 to back to main menu\n";
gotoxy(14,23);cout<<"Press 2 to quit from the program";
cin>>ans;
}while(ans=='1');
return 0;
}
char main_menu()
{
char choice;
gotoxy(14,10);cout<<"****** STUDENT INFORMATION SYSTEM ******\n";
gotoxy(14,11);cout<<"(1)View Record \n";
gotoxy(14,12);cout<<"(2)Add Record \n";
gotoxy(14,13);cout<<"(3)Del Record \n";
gotoxy(14,14);cout<<"Please enter your choice (1,2,or3) : ";
cin>>choice;
return(choice);
}