#include<fstream>

#include<string>

#include<iostream>

#include<iomanip>

#include <conio.h>

#include<string.h>

using namespace std;

class course{

public:

string CRN;

string title;

string department;

string room;

}; //course

class student{

public:

string fname, lname, major;

double gpa;

int courses[3];// course ids *primary key

}; //student

student e[100];

course c[100];

int n=0;

int z=0;

// student functions

void load(){

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

ifstream courseFile("courses.txt", ios::in);

while(studentFile>>e[n].fname>>e[n].lname>>e[n].major>>e[n].gpa>>e[n].courses[0]>>e[n].courses[1]>>e[n].courses[2]){

n++;}//WHILE

while(courseFile>>c[z].CRN>>c[z].title>>c[z].department>>c[z].room){

z++;}//WHILE

}//LOAD the student

void store(){

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

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

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

studentFile<<e[i].fname<<" "<<e[i].lname<<" "<<e[i].major<<" "<<e[i].gpa<<" "<<e[i].courses[0]<<" "<<e[i].courses[1]<<" "<<e[i].courses[2]<<endl;

//" "insert space between the words

}//FOR

ofstream courseFile("courses.txt", ios::out);

for(int j=0; j<z; j++){

if(c[j].CRN!=" ")

courseFile<<c[j].CRN <<" "<<c[j].title<<" "<<c[j].department<<" "<<c[j].room<<endl;

}//FOR

}//STORE

void insert(){

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

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

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

if(cin.fail()){

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

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

cout<<"Enter the first course ID: "; cin>>e[n].courses[0];

cout<<"Enter the second course ID: "; cin>>e[n].courses[1];

cout<<"Enter the third course ID: "; cin>>e[n].courses[2];

n++;

}//INSERTS THE RECORD TO ARRAY

void display(){

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

cout<<setw(10)<<"Major"<<setw(10)<<"GPA";

cout<<setw(10)<<"course 1"<<setw(10)<<"course 2"<<setw(10)<<"course 3";

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(15)<<e[i].fname<<setw(15)<<e[i].lname;

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

cout<<setw(10)<<e[i].gpa;

cout<<setw(10)<<c[e[i].courses[0]].title<<setw(10)<<c[e[i].courses[1]].title<<setw(10)<<c[e[i].courses[2]].title;

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;

}//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>>e[i].fname;

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

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

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

cout<<"Enter the 1st class ID:"; cin>>e[i].courses[0];

cout<<"Enter the 2nd class ID:"; cin>>e[i].courses[1];

cout<<"Enter the 3rd class ID:"; cin>>e[i].courses[2];

}//ELSE

}//UPDATE

// Course functions

void addcourse(){

cout<<"Enter course CRN: "; cin>>c[z].CRN;

cout<<"Enter course title: "; cin>>c[z].title;

cout<<"Enter course department: "; cin>>c[z].department;

if(cin.fail()){

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

cout<<"Enter course room: "; cin>>c[z].room;

z++;

}//addCourse

void displaycourse(){

cout<<setiosflags(ios::left)<<setw(15)<<"CRN"<<setw(15)<<"title";

cout<<setw(15)<<"department"<<setw(15)<<"room";

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(15)<<c[i].CRN<<setw(15)<<c[i].title;

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

cout<<setw(15)<<c[i].room;

cout<<resetiosflags(ios::left)<<endl;

}//IF

}//FOR

string departmentname;

cout<<"enter department to view courses:";

cin>>departmentname;

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

if(c[i].department==departmentname){

cout<<setiosflags(ios::left)<<setw(15)<<c[i].CRN<<setw(15)<<c[i].title;

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

cout<<setw(15)<<c[i].room;

cout<<resetiosflags(ios::left)<<endl;

}//IF

}//FOR

}//THIS FUNCTION DISPLAYS ALL courses

int searchcrn(string crn){

for(int i=0;i<z;i++) if(crn==c[i].CRN) return i;//FOR

return -1;

}//SEARCH BY CRN,-1 NOT FOUND

void updatecourse(){

string searchc;

cout<<"Enter the CRN of the course to be modified: "; cin>>searchc;

int i=searchcrn(searchc);

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

else{

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

cout<<"Enter course's CRN: "; cin>>c[i].CRN;

cout<<"Enter course's title: "; cin>>c[i].title;

cout<<"Enter course's department: "; cin>>c[i].department;

cout<<"Enter course's room: "; cin>>c[i].room;

}//ELSE

}//UPDATE the course

void report(){

string lname;

int i;

int j;

cout<<"Enter the student's last name:"<<endl; cin>>lname;

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

if(e[i].lname==lname) {

for(j=0;j<3;j++){

cout<<setiosflags(ios::left)<<setw(15)<<c[e[i].courses[j]].CRN<<setw(15)<<c[e[i].courses[j]].title;

cout<<setprecision(2)<<setiosflags(ios::left)<<setw(15)<<c[e[i].courses[j]].department;

cout<<setw(15)<<c[e[i].courses[j]].room;

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

} //if

} // for

}//REPORT

int login(){

char username[15], password[7];

int limit=0;

ifstream fin("login.txt");

int correct=0;//use as a flag

while(limit<3){

cout<<"\nPlease Enter your name: "<<endl;

cin>>username;

cout<<"\nPlease Enter your password (6 char) :"<<endl;

for(int i=0;i<6;i++){password[i] = getch(); cout<<'*';cout.flush();}//FOR

password[i]='\0';//last character to 0

char u[15], p[7]; //read from the login.txt

while(fin >> u >> p){

if(strcmp(username,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++;}//IF

}//WHILE

cout<<"\nSorry, you've used all your chances."<<endl;

return 0;

}//login

void main(){

if(login()){cout<<"\nWelcome to the SUNY OW Database!"<<endl;

//call login function

load();

char choice='z';

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

cout<<endl<<"SUNY OW Database"<<endl;

cout<<endl<<"student menu(s)"<<endl;

cout<<endl<<"course menu(c)"<<endl;

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

cin>>choice;

switch(choice){

case's':

cout<<endl<<"Student database"<<endl;

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

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

cout<<"3. Search by Last Name"<<endl;

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

cout<<"5. Display the student's courses"<<endl;

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

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

cin>>choice;

switch(choice){

case '1': insert(); break;

case '2': display(); break;

case '3': char s[100]; int index;

cout<<"Enter the last name of the student you want: ";cin>>s;

index=search(s);

if(index!=-1) {

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

cout<<"Major: "<<e[index].major<<endl;

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

}//IF

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

break;

case '4': update(); break;

case '5': report(); break;

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

default: cout<<"Invalid Choice"<<endl; break;

}//SWITCH

break;

case'c':

cout<<endl<<"Course database"<<endl;

cout<<"1. Add the Course"<<endl;

cout<<"2. Display All Couses by Department"<<endl;

cout<<"3. Search by CRN"<<endl;

cout<<"4. Update the Course"<<endl;

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

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

cin>>choice;

switch(choice){

case '1': addcourse(); break;

case '2': displaycourse();break;

case '3': char crn[100]; int index;

cout<<"Enter the CRN of the course you want: ";cin>>crn;

index=searchcrn(crn);

if(index!=-1) {

cout<<"CRN: "<<c[index].CRN<<endl;

cout<<"title: "<<c[index].title<<endl;

cout<<"department: "<<c[index].department<<endl;

cout<<"room: "<<c[index].room<<endl;}//IF

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

break;

case '4': updatecourse(); break;

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

default: cout<<"Invalid Choice"<<endl; break;

}//SWITCH

break;

case 'e': store(); break;

default: cout<<"Invalid Choice"<<endl; break;

} //switch

}//WHILE

} // if

else {cout<<"Sorry!You don't have the chance!"<<endl;}//else

} //main

Hosted by www.Geocities.ws

1