#include<fstream>
#include<string>
#include<string.h>
#include<iostream>
#include<iomanip>
#include <conio.h>
using namespace std;
class course{
public:
string CRN, title, dept, room;};
class Students{ // student class has an object course inside it.
public:
string fname, lname, Major;
double GPA;
int Credits;
int courseId[4];}; //course ID's *primary key*
//Students
Students e[100];
course c[100];
int n=0;
int z=0;
void load(){
ifstream sin("Students.txt",ios::in);// ios::in specify that it is opening the
file
ifstream cin("Course.txt",ios::in);
while(sin>>e[n].fname>>e[n].lname>>e[n].Major>>e[n].Credits>>e[n].GPA>>e[n].courseId[0]>>e[n].courseId[1]>>e[n].courseId[2]>>e[n].courseId[3]){
n++;}//WHILE // )
while (cin>>c[z].CRN>>c[z].title>>c[z].dept>>c[z].room){
z++;}
}
//LOAD
// file courses.txt, inserts, search( By course name and Number), display...
//classes and struct has ; at the end...function dont
void store(){
ofstream fout("Students.txt",ios::out);
ofstream courseout("Course.txt",ios::out);
for(int i=0; i<n; i++){
if(e[i].fname!=" ")
fout<<e[i].fname<<" "<<e[i].lname<<" "<<e[i].Major<<" "<<e[i].Credits<<"
"<<e[i].GPA<<" "<<e[i].courseId[0]<<" "<<e[i].courseId[1]<<"
"<<e[i].courseId[2]<<" "<<e[i].courseId[3]<<endl;
}
for(int j=0;j<z; j++){
if(c[j].CRN!="0")
courseout<<c[j].CRN<<" "<<c[j].title<<" "<<c[j].dept<<" "<<c[j].room<<endl;
}//FOR
}
//STORE
void insert(){
cout<<"Enter Students's first name: "; cin>>e[n].fname;
cout<<"Enter Students's last name: "; cin>>e[n].lname;
cout<<"Enter Student's Major: "; cin>>e[n].Major;
cout<<"Enter Student's Credits: "; cin>>e[n].Credits;
cout<<"Enter Student's GPA: "; cin>>e[n].GPA;
cout<<"Please Enter the course ID(s)(0,1,2,3,4):"; cin>>e[n].courseId[0];
cout<<"Please Enter the course ID(s)(0,1,2,3,4):"; cin>>e[n].courseId[1];
cout<<"Please Enter the course ID(s)(0,1,2,3,4):"; cin>>e[n].courseId[2];
cout<<"Please Enter the course ID(s)(0,1,2,3,4):"; cin>>e[n].courseId[3];
n++; }
void insertcourse(){
cout<<"Enter Cource CRN: "; cin>>c[z].CRN;
cout<<"Enter the Title of the Course: "; cin>>c[z].title;
cout<<"Enter the name of Department: "; cin>>c[z].dept;
cout<<"Enter the Room No.: "; cin>>c[z].room;
z++; }
//INSERTS THE RECORD TO ARRAY
void display(){
system("cls");
cout<<setiosflags(ios::left)<<setw(12)<<"First Name"<<setw(12)<<"Last Name";
cout<<setw(7)<<"Major"<<setw(10)<<"Credits"<<setw(8)<<"GPA"<<"courseId";
cout<<endl<<endl;
cout<<setiosflags(ios::fixed|ios::showpoint|ios::left);
for(int i=0;i<n;i++){
if(e[i].fname!=" "){
cout<<setiosflags(ios::left)<<setw(12)<<e[i].fname<<setw(12)<<e[i].lname;
cout<<setprecision(2)<<setiosflags(ios::left)<<setw(7)<<e[i].Major;
cout<<setw(10)<<e[i].Credits<<setw(8)<<e[i].GPA<<e[i].courseId[0]<<"
"<<e[i].courseId[1]<<" "<<e[i].courseId[2]<<" "<<e[i].courseId[3];
cout<<resetiosflags(ios::left)<<endl;}//IF
}//FOR
}
void addCourse( ){
cout<<"Enter course CRN: ";
cin>>c[z].CRN;
cout<<"Enter course title: ";
cin>>c[z].title;
cout<<"Enter course room: ";
cin>>c[z].room;
cout<<"Enter course department: ";
cin>>c[z].dept;
z++;
}//addCourse
int searchCourse(string crn){
for(int i=0;i<z;i++) if(crn==c[i].CRN) return i;//FOR
return -1;
}//SEARCH COURSE BY CRN
void displaycourse(){
system("cls");
cout<<setiosflags(ios::left)<<setw(12)<<"Course Number"<<setw(12)<<"Title";
cout<<setw(7)<<"Department"<<setw(7)<<"Room"<<endl;
cout<<endl<<endl;
cout<<setiosflags(ios::fixed|ios::showpoint|ios::left);
for(int i=0;i<n;i++){
if(c[i].CRN!=" "){
cout<<setiosflags(ios::left)<<setw(12)<<c[i].CRN<<setw(12)<<c[i].title;
cout<<setprecision(2)<<setiosflags(ios::left)<<setw(7)<<c[i].dept;
cout<<setw(7)<<c[i].room;
cout<<resetiosflags(ios::left)<<endl;}//IF
}//FOR
}
//THIS FUNCTION DISPLAYS ALL RECORDS IN ARRAY
int search(string s){
for(int i=0;i<n;i++)if(s==e[i].lname)return i;//FOR
return -1;
}
/*int searchCourse (string crn){
for (int i=0; i<z;i++)
if (crn==c[i].CRN)
return i;
// for loop
return -1;}*/
// return -1 when the recortd is not found
//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<<"Students NOT LISTED"<<endl;
else{
cout<<endl<<"Please enter the updated information."<<endl;
cout<<"Enter Students's first name: "; cin>>e[i].fname;
cout<<"Enter Students's last name: "; cin>>e[i].lname;
cout<<"Enter Students's Major: "; cin>>e[i].Major;
cout<<"Enter Students's Credits: "; cin>>e[i].Credits;
cout<<"Enter Students's GPA: "; cin>>e[i].GPA;
}
}
//UPDATE
void deleterec(){
string searchn;
cout<<"Enter the Last Name of the Students to delete: "; cin>>searchn;
int k=-1;
for(int j=0; j<n;j++){
if(searchn==e[j].lname)
k=j;}
if(k!=-1)
{ cout<<" Name: "<<e[k].fname<<" "<<e[k].lname<<endl;
cout<<" Major: "<<e[k].Major<<endl;
cout<<" Credits: "<<e[k].Credits<<endl;
cout<<" GPA: "<<e[k].GPA<<endl<<endl;}
char ans;
cout<<endl<<"Do you want to delete this record? (Y/N): ";
cin>>ans;
if(ans=='Y'||ans=='y'){
e[k].fname=" ";}//IF
else cout<<"STUDENT NOT FOUND"; }
//DELETE
// Report
void report(){// Average GPA, Highest GPA, Total number of Students
int i=0;
double totalgpa=0.00, avg;
for(i=0;i<n;i++){
totalgpa=totalgpa + e[i].GPA;}
avg=totalgpa/n;
cout<<search<<endl;
cout<<"Total number of Students: " <<n<<endl;
cout<<"Average GPA: "<<avg<<endl<<endl;}
// report for transcript
void Transcript() {
char s[100];
int index;
cout<<"Enter the last name of the person you want: "<<endl;
cin>>s;
index=search(s);
cout<<endl<<endl<<endl;
system("cls");
cout<<"**************** Student Transcript
*****************"<<endl<<endl<<endl<<endl;
if(index!=-1) {
cout<<"Name: "<<e[index].fname<<" "<<e[index].lname<<endl;
cout<<"Student's Major: "<<e[index].Major<<endl;
cout<<"Total Credits: "<<e[index].Credits<<endl;
cout<<"Net GPA: "<<e[index].GPA<<endl<<endl;}
cout<<setiosflags(ios::left)<<setw(15)<<"Course Number "<<setw(10)<<"Title
"<<setw(13)<<"Department "<<setw(17)<<"Room Number "<<endl<<endl;
cout<<setiosflags(ios::left)<<setw(15)<<c[e[index].courseId[0]].CRN<<setw(10)<<c[e[index].courseId[0]].title<<setw(13)<<c[e[index].courseId[0]].dept<<setw(13)<<c[e[index].courseId[0]].room<<endl;
cout<<setiosflags(ios::left)<<setw(15)<<c[e[index].courseId[1]].CRN<<setw(10)<<c[e[index].courseId[1]].title<<setw(13)<<c[e[index].courseId[1]].dept<<setw(13)<<c[e[index].courseId[1]].room<<endl;
cout<<setiosflags(ios::left)<<setw(15)<<c[e[index].courseId[2]].CRN<<setw(10)<<c[e[index].courseId[2]].title<<setw(13)<<c[e[index].courseId[2]].dept<<setw(13)<<c[e[index].courseId[2]].room<<endl;
cout<<setiosflags(ios::left)<<setw(15)<<c[e[index].courseId[3]].CRN<<setw(10)<<c[e[index].courseId[3]].title<<setw(13)<<c[e[index].courseId[3]].dept<<setw(13)<<c[e[index].courseId[3]].room<<endl;
//cout<<setiosflags(ios::left)<<setw(15)<<c[index].CRN<<setw(8)<<c[index].title<<setw(13)<<c[index].dept<<setw(13)<<c[index].room<<endl;
cout<<endl<<endl;
}
void deletefile(){
int pword;
cout<<"Enter your administrative password: "; cin>>pword;
if(pword==5438){
ofstream record("Students.txt", ios::out);
record<<endl;
load();
record.close();}
else cout<<"Wrong password entered."<<endl; }//DELETE FILE
void backupfile(){
system("copy Students.txt backup.txt");
cout<<endl<<"Back-up file has been made."<<endl;}
//BACKUPFILE use cp command for UNIX
void studentmenu(){
char choice='z';
system("cls");
cout<<"************Students Menu*************"<<endl<<endl;
cout<<"1. Insert Students Record"<<endl;
cout<<"2. Display All Records"<<endl;
cout<<"3. Search by Last Name"<<endl;
cout<<"4. Search by Student ID"<<endl;
cout<<"5. Update Record"<<endl;
cout<<"6. Delete Record"<<endl;
cout<<"7. Generate Report"<<endl;
cout<<"8. Delete Database File"<<endl;
cout<<"9. Back-up Database File"<<endl;
cout<<"0. Student Transcript"<<endl<<endl<<endl;
cout<<"Type e to exit"<<endl<<endl;
cout<<"Enter the number of your choice:"<<endl;
cin>>choice; // input statements
switch (choice){
case '1': insert(); break;
case '2': display(); break;
case '3':
char s[100]; int index;
cout<<"Enter the last name of the person you want: "<<endl;
cin>>s;
index=search(s);
if(index!=-1) {
cout<<"Name: "<<e[index].fname<<" "<<e[index].lname<<endl;
cout<<"Student's Major: "<<e[index].Major<<endl;
cout<<"Total Credits: "<<e[index].Credits<<endl;
cout<<"Net GPA: "<<e[index].GPA<<endl<<endl;}//IF
else cout<<"Students NOT FOUND"<<endl;
break;
case '4':
int id;
cout<<"Enter Student ID ";
cin>>id;
if(id < n){
cout<<"Name: "<<e[id].fname<<" "<<e[id].lname<<endl;
cout<<"Student's Major: "<<e[id].Major<<endl;
cout<<"Total Credits: "<<e[id].Credits<<endl;
cout<<"Net GPA: "<<e[id].GPA<<endl<<endl;}
else cout<<" Student Not Found"<<endl;
break;
case '5': update(); break;
case '6': deleterec(); break;
case '7': report(); break;
case '8': deletefile(); break;
case '9': backupfile(); break;
case '0': Transcript(); break; //EXIT
case 'e': store(); break; //EXIT
default: cout<<"Invalid Choice"<<endl; break; }//SWITCH
}
int login(){
char usrname[15], password[10]; int limit=0;
ifstream fin("login.txt");
int correct=0;
while(limit<=3) {
cout<<"\nPlease Enter user name: "<<endl;
cin>>usrname;
cout<<"\nPlease Enter your password (6 char) :"<<endl;
for(int i=0;i<6;i++){password[i] = getch(); cout<<'*'; cout.flush();}//FOR
password[6]=0;
char u[15], p[10];
while(fin >> u >> p){
if(strcmp(usrname,u)==0 && strcmp(password, p)==0){
cout<<"\nYour Password is ACCEPTED"<<endl;
correct=1;
return 1;
}//IF
}//WHILE
if(correct==0){cout<<"\nYour Password is incorrect. Please try again."<<endl;
limit++;}//ELSE
}//WHILE
cout<<"\nSorry, you've used all your chances."<<endl;
return 0;
}//Login
void coursemenu(){
char choice='z';
system("cls");
cout<<"***************Course Menu*****************"<<endl<<endl;
cout<<"1. Insert Course Information"<<endl;
cout<<"2. Display All Records"<<endl;
cout<<"3. Search by Course Number"<<endl;
//cout<<"2. Insert Course Title"<<endl;
//cout<<"3. Insert Class Room"<<endl;
//cout<<"4. Insert Department Name"<<endl;
cin>>choice; // input statements
switch (choice){
case '1': insertcourse(); break;
case '2': displaycourse(); break;
// case '3': break;
case '3':// search for course table..
char crn[100]; int index;
cout<<"Enter the CRN of the course: ";
cin>>crn;
index=searchCourse(crn);
if(index!=-1) {
cout<<"CRN: "<<c[index].CRN<<endl;
cout<<"Title: "<<c[index].title<<endl;
cout<<"Room: "<<c[index].room<<endl;
cout<<"Department: "<<c[index].dept<<endl;
}//IF
else cout<<"COURSE NOT FOUND"<<endl;
break;break;
default: cout<<"Invalid Choice"<<endl; break;
}
}
void main(){
login(); // LOGIN PROGRAM
load();
char choice='z';
while (choice!='e'&&choice!='E'){
cout<<endl<<"Student Database"<<endl;
cout<<endl<<"Student Menu(S)"<<endl;
cout<<endl<<"Course Menu(C)"<<endl<<endl<<endl;
cout<<endl<<"Save Database(x)"<<endl<<endl<<endl;
cout<<"Type e to exit"<<endl<<endl;
cin>>choice;
switch(choice){
case's':
studentmenu();
break;
case'c':
coursemenu();
break;
case'x':
store();
break;
case'e':
store();
break;
// student database
}}
}
//WHILE
//MAIN