View Single Post
  #1  
Old 11-12-2008, 09:54 AM
T-INFECTION's Avatar
T-INFECTION T-INFECTION is offline
ATTIQUE ATTA

 
Join Date: May 2008
Location: MULTAN
Posts: 6
Contact Number: 0333-4671561*,0345-2870285
Program / Discipline: BSTS
Class Roll Number: BSTS-08-30
T-INFECTION is on a distinguished road
Send a message via MSN to T-INFECTION
Sunny Filling in oop c++

#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
class Student
{
private:
int rollno;
char name[30];
double marks;
public:
Student()
{
rollno = 0;
strcpy(name, "");
marks = 0;
}
void InputData()
{
cout << "Enter the rollno:";
cin >> rollno;
cin.ignore (256, '\n');
cout << "Enter the name: ";
cin.getline(name, 30);
cout << "Enter the marks: ";
cin >> marks;
}
void DisplayData()
{
cout << "Rollno: " << rollno << endl;
cout << "Name: " << name << endl;
cout << "Marks: " << marks << endl;
}
};
void CreateFile()
{

ofstream os("Student.dat",ios::out|ios::binary);
if (!os)
{
cout << "Unable to create file" << endl;
exit(1);
}
cout << "Successfully File Created" << endl;
}
void AddRecord()
{
Student s;

ofstream os("Student.dat",ios::app|ios::binary);
if (!os)
{
cout << "Unable to open file" << endl;
exit(1);
}
int n;
cout << "Enter the no of records to store:";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "Enter the student Info" << endl;
s.InputData();
os.write((char *) &s, sizeof(s));
cout << "Record Stored Successfully" << endl;
}
os.close();
} // end of Add Record
void DisplayRecord()
{
Student s;
/* Statement to read records from student file */
ifstream is("Student.dat",
ios::in|ios::binary);
if (!is)
{
cout << " Unable to open file or File not found" << endl;
exit(1);
}
while (!is.eof())
{
is.read((char*) &s, sizeof(s));
if (!is.eof())
s.DisplayData();
}
is.close();
} // end of Dislay Record
int main()
{
int opt;
while (1)
{
cout << "!!!!!!!Student Database System!!!!!!!" << endl;
cout << "1: Create Student File " << endl;
cout << "2: Add Records " << endl;
cout << "3: Display Records " << endl;
cout << "4: Exit " << endl;
cout << "Ente the option: ";
cin >>opt;
if (opt == 1)
CreateFile();
else if (opt == 2)
AddRecord();
else if (opt == 3)
DisplayRecord();
else if (opt == 4)
break;
else
cout << "Invalid option" << endl;
} // end of while loop

return 0;
}
//M.ATTIQUE ATTA
//333-4671561




Reply With Quote