i have a small problem , when i exit the program ,open it again and load ( nothing loaded !!!!) i select display .. nothin is displayed.. i don't know if the problem is in the save() function or in the load() function ....
ThanX
Code:
#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
struct student
{
char fname[100];
char lname[100];
int id;
float cgpa;
};
int add( student * , int &);
void display( student * , int );
void save(student * , int );
void load( student * , int );
int main(int argc, char *argv[])
{
char x;
student info [200 ];
int i = 0 , num , max = 0;
do
{
cout <<"============================\n";
cout <<" Menu \n";
cout <<"============================\n";
cout <<endl;
cout <<"={1}= Add a new student.\n";
cout <<"={2}= Display all\n";
cout <<"={3}= Save\n";
cout <<"={4}= Load\n";
cout <<"={5}= Exit\n";
cout <<"============================\n";
cout <<"Your Choice: ";
cin >>x;
switch ( x )
{
case '1' : add( info , i );
break;
case '2' : display( info , i);break;
case '3' : save( info , i);break;
case '4' : load( info , i );break;
case '5' : ;break;
default : cout <<"Invalid Choice\n";
}
}while ( x != '5' );
system("PAUSE");
return 0;
}
//================================================== ===
//================================================== ==========
// Add a new student to the database
int add(student * info , int & i )
{
if( i < 200 )
{
cout << "Enter student's first name: ";
cin >>info[i].fname;
cout << "Enter student's second name: ";
cin >>info[i].lname;
cout <<"Enter the student id: ";
cin >> info[i].id;
cout << "Enter the student CGPA: ";
cin >> info[i].cgpa;
i++;
}
else
{
cout << "No more room in array!" << endl;
}
return 0;
}
//===============================[ display ]=======================
void display( student * info , int max )
{
int i = 0;
while ( i < max )
{
cout << "Name: "<< info [ i ].fname << " " << info [ i ].lname << endl;
cout << "ID: " << info [ i ].id << endl;
cout << "CGPA: "<< info [ i ].cgpa << endl;
i ++;
}
}
//========================[ save ]=============================
void save( student * info , int i)
{
ofstream fout;
fout.open("datab.txt",ios::out|ios::binary);
fout.write(reinterpret_cast<char*>(&info[i]),sizeof(student));
fout.close();
}
//=======================[load]=================================
void load( student * info , int i)
{
ifstream fin;
fin.open("datab.txt",ios::in|ios::binary);
fin.read(reinterpret_cast<char*>(&info[i]),sizeof(student));
fin.close();
}