Student Database. how to read from the file. the load function dosent worl.

#include <iostream>
#include <iomanip>
#include <cstring>
#include <fstream>
#include <string>

using namespace std;

struct stu_data
{
int id;
string fn;
string ln;
char gender;
string major;
float cgpa;
};

void load (stu_data array[], int & n, ifstream & extract);
void insertion (stu_data array[], int & n, ifstream & extract);
void deletion (stu_data array[], int & n);
void searching (stu_data array[], int & n);
void printing (stu_data array[], int & n);
void dump (stu_data array[], int & n, ofstream & write);

int main()
{
string s;
ifstream read;
ifstream extract;
ofstream write;

cout<<"Enter the name of the data file: ";
cin>>s;

read.open(s.c_str());
if (!read)
{
cout<<"Input file open error\n";
}

write.open("a.txt");
if(!write)
{
cout<<"Output file open error\n";
}


cout<<"\n\t\tStudent Database.";
cout<<"\n\nNB: Enter full name, full ID, entire CGPA, abreviated Gender \n"
<<" (M for Male or F for Female) and abreviated Major (EEE, CSE, etc), \n"
<<" for double major use comma in-between, followed by a space.\n\n";

for (int x = 0; x < 70; x++)
{
cout<<"|";
}

cout<<endl<<endl;

int stu_n = 0;
stu_data array[20];

for (char quit = 'a'; quit != 'q'; )
{
char choice = 'z';

cout<<"\nEnter choices...............\n"
<<"To insert data...... press i\n"
<<"To delete data.......press d\n"
<<"To search data.......press s\n"
<<"To print data........press p\n"
<<"To quit program......press q\n"
<<"Enter choice now............: ";
cin>>choice;

cout<<endl;

// insertion
if (choice == 'i')
{
insertion (array, stu_n, extract);
}

// deletion
else if (choice == 'd')
{
deletion (array, stu_n);
}

// searching
else if (choice == 's')
{
searching (array, stu_n);
}

// printing
else if (choice == 'p')
{
printing (array, stu_n);
}

// exit
else if (choice == 'q')
{
dump (array, stu_n, write);
quit = choice;
}

// invalid
else
{
cout<<"Invalid Input\n";

}
}

return 0;
}

void load (stu_data array[], int & n, ifstream & extract)
{
extract.open("a.txt");
int id_n;
string fn_n;
string ln_n;
char gen;
string maj;
float gpa;

while(extract>>id_n>>fn_n>>ln_n>>gen>>maj>>gpa)
{
array[n].id = id_n;
array[n].fn = fn_n;
array[n].ln = ln_n;
array[n].gender = gen;
array[n].major = maj;
array[n].cgpa = gpa;

n = n + 1;
}
}

void insertion (stu_data array[], int & n, ifstream & extract)
{
load (array, n, extract);
const int z = 40;

// ID
cout<<" ID: ";
cin>>array[n].id;

// First Name
cout<<" First Name: ";
cin>>array[n].fn;

// Last Name
cout<<" Last Name: ";
cin>>array[n].ln;

// Gender
cout<<" Sex: ";
cin>>array[n].gender;

// GPA
cout<<"CGPA : ";
cin>>array[n].cgpa;

// Major
cout<<"Major: ";
cin>>array[n].major;

int id_n;
string f_n;
string l_n;
char gen;
string maj;
float gpa;

for (int i = n - 1; i >= 0; i--)
{
if (array[i + 1].fn.compare(array[i].fn) < 0)
{
id_n = array[i].id;
array[i].id = array[i + 1].id;
array[i + 1].id = id_n;

f_n = array[i].fn;
array[i].fn = array[i + 1].fn;
array[i + 1].fn = f_n;

l_n = array[i].ln;
array[i].ln = array[i + 1].ln;
array[i + 1].ln = l_n;

gen = array[i].gender;
array[i].gender = array[i + 1].gender;
array[i + 1].gender = gen;

maj = array[i].major;
array[i].major = array[i + 1].major;
array[i + 1].major = maj;

gpa = array[i].cgpa;
array[i].cgpa = array[i+1].cgpa;
array[i+1].cgpa = gpa;
}
}

n = n + 1;
}

void deletion (stu_data array[], int & n)
{
char delet;

cout<<"To delete using ID .......press........i\n"
<<"................Name......press .......n\n"
<<"Enter now.......: ";
cin>>delet;

int i;
if (delet == 'i') // id delete
{
int delet_i;
int box;

int bread_crum = -1;

cout<<"Provide the id of the student you want to remove: ";
cin>>delet_i;

for(i = 0; i < n; i++)
{
if(array[i].id == delet_i)
{
box = i;
bread_crum = 1;
break;
}
}

if (bread_crum > 0)
{
array[box].id = array[n - 1].id;
array[box].fn = array[n - 1].fn;
array[box].ln = array[n - 1].ln;
array[box].gender = array[n - 1].gender;
array[box].major = array[n - 1].major;
array[box].cgpa = array[n - 1].cgpa;

n--;

cout<<"\nStudent information has been deleted!\n";
}
else
{
cout<<"\nStudent cant be deleted, there is no such record.\n";
}
}


}

void searching (stu_data array[], int & n)
{
const int z = 40;
char search = 'x';
int s_id;

int i,j;

cout<<"To search using Name.....press n\n"
<<"To search using ID.......press i\n"
<<"Enter now......:";

cin.clear();
cin.ignore();
cin>>search;


// id search
else if (search == 'i')
{
cout<<"Enter the ID for searching: ";
cin>>s_id;

int x = 0;

for (j = 0; j <= n; j++)
{
if (array[j].id == s_id)
{
x = j;
break;
}
}

if (array[x].id == s_id)
{
cout<<"\nID has been found\n";
}

else
{
cout<<"\nID is not in the database\n";
}
}
}
void printing (stu_data array[], int & n)
{
int x, i;

cout<<"ID"<<setw(15)<<"Names"<<setw(20)<<"Sex"<<setw(7)<<"CGPA"<<setw(9)<<"Major";
cout<<endl;

for(i = 0; i < n; i++)
{
cout<<array[i].id<<"";
cout<<setw(5)<<"";

cout<<array[i].fn<<"";
cout<<" "<<array[i].ln<<"";
x = array[i].ln.length() + array[i].fn.length();
cout<<setw(22 - x)<<"";


cout<<array[i].gender<<"";
cout<<setw(4)<<"";

cout<<showpoint<<setprecision(3)<<array[i].cgpa<<"";
cout<<setw(5)<<"";

cout<<array[i].major<<"";
x = array[i].major.length();
cout<<setw(10 - x)<<"";

cout<<endl;
}
}
void dump (stu_data array[], int & n, ofstream &write)
{
int i;

for(i = 0; i < n; i++)
{
write<<array[i].id;
write<<array[i].fn;
write<<array[i].ln;
write<<array[i].gender;
write<<array[i].cgpa;
write<<array[i].major<<endl;
}
}
Use code tags. "dosent werl" is not a valid problem description.
Topic archived. No new replies allowed.