#include<fstream>

#include<string>

#include<iostream>

#include<iomanip>

using namespace std;

class student{

public:

string fname;

string lname;

string major;

double gpa;

}; //student

student s[100];

int n=0;

void load(){

ifstream fin("students.txt",ios::in);

while(fin>>s[n].fname>>s[n].lname>>s[n].major>>s[n].gpa){

n++;}//WHILE

}//LOAD

void store(){

ofstream fout("students.txt",ios::out);

for(int i=0; i<n; i++){

if(s[i].fname!=" ")

fout<<s[i].fname<<" "<<s[i].lname<<" "<<s[i].major<<" "<<s[i].gpa<<endl;

}//FOR

}//STORE

void insert(){

cout<<"Enter student's first name: "; cin>>s[n].fname;

cout<<"Enter student's last name: "; cin>>s[n].lname;

cout<<"Enter student's major: "; cin>>s[n].major;

if(cin.fail())

{cout<<"Error\n"; exit(1);}

cout<<"Enter student's gpa: "; cin>>s[n].gpa;

n++;

}//INSERTS THE RECORD TO ARRAY

void display(){

cout<<setiosflags(ios::left)<<setw(15)<<"First Name"<<setw(15)<<"Last Name";

cout<<setw(15)<<"major"<<setw(15)<<"gpa";

cout<<endl<<endl;

cout<<setiosflags(ios::fixed|ios::showpoint|ios::left);

for(int i=0;i<n;i++){

if(s[i].fname!=" "){

cout<<setiosflags(ios::left)<<setw(15)<<s[i].fname<<setw(15)<<s[i].lname;

cout<<setprecision(2)<<setiosflags(ios::left)<<setw(15)<<s[i].major;

cout<<setw(15)<<s[i].gpa;

cout<<resetiosflags(ios::left)<<endl;}//IF

}//FOR

}//THIS FUNCTION DISPLAYS ALL RECORDS IN ARRAY

int search(string name){

for(int i=0;i<n;i++)if(name==s[i].lname)return i;

return -1;

}//SEARCH BY NAME,-1 NOT FOUND

void update(){

string searchn;

cout<<"Enter the Last Name of the Student to be modified: "; cin>>searchn;

int i=search(searchn);

if(i==-1) cout<<"STUDENT NOT LISTED"<<endl;

else{

cout<<endl<<"Please enter the updated information."<<endl;

cout<<"Enter student's first name: "; cin>>s[i].fname;

cout<<"Enter student's last name: "; cin>>s[i].lname;

cout<<"Enter major: "; cin>>s[i].major;

cout<<"Enter gpa: "; cin>>s[i].gpa;

}//ELSE

}//UPDATE

void main(){

load();

char choice='0';

while (choice!='e'&&choice!='E'){

cout<<endl<<"Students Database"<<endl;

cout<<"1. Insert Student Record"<<endl;

cout<<"2. Display All Records"<<endl;

cout<<"3. Search Database"<<endl;

cout<<"4. Update Record"<<endl;

cout<<endl<<" Type e to exit"<<endl<<endl;

cout<<"Enter the number of your choice:";

cin>>choice; cout<<endl;

switch (choice){

case '1': insert(); break;

case '2': display(); break;

case '3': char m[100];

int index;

cout<<"Enter the last name of the person you want: ";

cin>>m;

index=search(m);

if(index!=-1) {

cout<<"Name: "<<s[index].fname<<" "<<s[index].lname<<endl;

cout<<"major: "<<s[index].major<<endl;

cout<<"GPA: "<<s[index].gpa<<endl;

}//IF

else cout<<"Student NOT FOUND"<<endl;

break;

case '4': update(); break;

case 'e': store(); break; //EXIT

default: cout<<"Invalid Choice"<<endl; break; }//SWITCH

}//WHILE

}//MAIN

Hosted by www.Geocities.ws

1