Created
November 27, 2016 12:06
-
-
Save stinkymonkeyph/91afb2b7de66eeb1b89f541a597b11a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @author : Nelmin Jay Magan Anoc | |
* file handling script to retrieve a simple classlist on a text file | |
*/ | |
#include<iostream> | |
#include<fstream> | |
#include<string> | |
using std::cout; | |
using std::cin; | |
using std::string; | |
using std::ifstream; | |
using std::ofstream; | |
using std::endl; | |
const string FILE_NAME = "classlist.txt"; | |
const string CLEAR_SCREEN = "\033[2J"; // ansi escape code for clearing a screen inside a linux terminal | |
const string CURSOR_UP = "\033[1;1H"; // ansi escape code for getting the cursor up on the screen | |
int getMenu(); | |
void transaction(int); | |
void showStudents(ifstream &); | |
void addStudent(ofstream &); | |
void searchStudent(ifstream &); | |
void deleteStudent(ifstream &); | |
int main() | |
{ | |
char repeat ; | |
do | |
{ | |
cout << CURSOR_UP ; | |
cout << CLEAR_SCREEN ; | |
transaction(getMenu()); | |
cout << "Return to main menu ? (y/n): "; | |
cin >> repeat ; | |
} | |
while(repeat == 'Y' || repeat == 'y'); | |
return 0; | |
} | |
void transaction(int menu_choice) | |
{ | |
ofstream class_list_of(FILE_NAME.c_str(), std::ios_base::app | std::ios_base::out); | |
ifstream class_list_if(FILE_NAME.c_str()); | |
switch (menu_choice) | |
{ | |
case 1 : | |
addStudent(class_list_of); | |
break; | |
case 2 : | |
showStudents(class_list_if); | |
break; | |
case 3 : | |
searchStudent(class_list_if); | |
break; | |
case 4 : | |
deleteStudent(class_list_if); | |
break; | |
} | |
class_list_if.close(); | |
class_list_of.close(); | |
} | |
int getMenu() | |
{ | |
int menu_choice = 0; | |
cout << "**---------------------------------**" << endl ; | |
cout << "This is a simple class set program \n" ; | |
cout << "**---------------------------------**" ; | |
cout << endl << endl ; | |
cout << "~~choose from the menu below : \n" ; | |
cout << endl ; | |
cout << "(1) Add a student \n"; | |
cout << "(2) View student list \n"; | |
cout << "(3) Search student \n"; | |
cout << "(4) Delete student \n"; | |
cin>> menu_choice; | |
cin.get(); | |
cout << endl << "~~~~end choosing from the menu~~~~" << endl ; | |
return menu_choice; | |
} | |
void addStudent(ofstream &class_list) | |
{ | |
cout << CURSOR_UP ; | |
cout << CLEAR_SCREEN ; | |
string name_student , name_instructor ; | |
cout << "~~~~add student to class list~~~~" << endl << endl ; | |
cout << "Enter name of the student: "; | |
getline(cin, name_student , '\n'); | |
cout << "Enter name of the instructor: "; | |
getline(cin, name_instructor, '\n'); | |
class_list << name_student << " | " << name_instructor <<" \n"; | |
class_list.close(); | |
cout << endl << "~~~~end adding student to class list~~~~" << endl ; | |
} | |
void showStudents(ifstream &class_list) | |
{ | |
cout << CURSOR_UP ; | |
cout << CLEAR_SCREEN ; | |
string get_content ; | |
string delimiter = "|"; | |
int student_record_counter = 0; | |
cout << "~~~~showing class list~~~~" << endl << endl ; | |
while(getline(class_list, get_content)) | |
{ | |
string name_student = get_content.substr(0, get_content.find(delimiter)); | |
string name_instructor = get_content.substr(name_student.size()+1, get_content.size()); | |
name_instructor.replace(name_instructor.length()-1,name_instructor.length(),""); | |
cout << student_record_counter+1 << "). " << name_student << " is under " << name_instructor << endl; | |
student_record_counter++; | |
} | |
cout << endl << "~~~~end showing class list~~~~" << endl ; | |
} | |
void deleteStudent(ifstream &class_list) | |
{ | |
cout << CURSOR_UP ; | |
cout << CLEAR_SCREEN ; | |
string name_student = "" ; | |
string get_content = ""; | |
ofstream temp; | |
temp.open("temp.txt"); | |
cout << "~~~~delete a student~~~~" << endl << endl ; | |
cout << "student name : " ; | |
getline(cin, name_student, '\n'); | |
while (getline(class_list,get_content)) | |
{ | |
if (!(get_content.find(name_student) != string::npos)) | |
{ | |
temp << get_content << endl; | |
} | |
} | |
temp.open("temp.txt"); | |
remove("classlist.txt"); | |
rename("temp.txt","classlist.txt"); | |
cout << endl << "~~~~end student deletion~~~~" << endl ; | |
} | |
void searchStudent(ifstream &class_list) | |
{ | |
cout << CURSOR_UP ; | |
cout << CLEAR_SCREEN ; | |
string name_student = "" ; | |
string name_instructor = "" ; | |
string get_content = "" ; | |
cout << "~~~~search a student and check if it is under an instructor~~~~" << endl << endl ; | |
cout << "Student name : " ; | |
getline(cin, name_student, '\n'); | |
cout << "Instructor name : " ; | |
getline(cin, name_instructor, '\n'); | |
while(getline(class_list, get_content)) | |
{ | |
if(get_content.find(name_student) != string::npos) | |
{ | |
string parse_name_student = get_content.substr(0, get_content.find("|")); | |
string parse_name_instructor = get_content.substr(parse_name_student.size()+1, get_content.size()); | |
parse_name_instructor.erase(parse_name_instructor.begin()); | |
if(parse_name_instructor.find(name_instructor) != string::npos) | |
{ | |
cout << endl << name_student << " is under " << name_instructor << endl ; | |
} | |
else | |
{ | |
cout << endl << name_student << " is not under " << name_instructor << endl ; | |
cout << "but under " << parse_name_instructor << endl ; | |
} | |
} | |
} | |
cout << endl << "~~~~end searching a student~~~~" << endl ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment