Jery
12-22-2005, 02:52 AM
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct student{
string id;
string name;
double attendance;
double assignment;
double quiz;
double midterm;
double practical;
double finalexam;
};
// creates an array for student
student studentList[20];
// declaration of the fuctions
student createStudent(void);
void addStudent(student [], student);
student searchStudent(student[]);
void removeStudent(student[], student);
void showStudent(student);
void takeExam(student);
void calculateMark(student);
// loads and saves a file
void saveStudent(student[]);
void loadStudent(student[]);
int main (void){
// cleans the array before adding data into them
for (int i=0; i<20; i++){
studentList[i].id = "xxxxxx";
}
student s;
// creates a display menu for users
int choice;
do{
system("CLS");
cout << "[1] Add a student." << endl;
cout << "[2] Delete a student." << endl;
cout << "[3] Search for a student by ID." << endl;
cout << "[4] Check if the student is able to take the exam (by ID)." << endl;
cout << "[5] Determine the grade for the student (by ID)." << endl;
cout << "[6] Save student to file" << endl;
cout << "[7] Load student to file" << endl;
cout << "[8] Exit." << endl;
cout << "Enter your choice" << endl;
cin >> choice;
cin.ignore();
// creates a switch case
switch(choice){
case 1:
/* creating a student
1. asking user for information about the student.
2. saving the information into the array.
*/
s = createStudent();
addStudent(studentList, s);
break;
case 2:
/* deleting a student
1. ask users for the id of the student.
2. search for the id in the array.
3. removing the information
*/
s = searchStudent(studentList);
removeStudent(studentList, s);
break;
case 3:
/* searching for a student by id and displaying information
1. get user input (id) and searching it in the array.
2. create a method to display the information
*/
s = searchStudent(studentList);
showStudent(s);
break;
case 4:
/* Verifying if the student is able to take the exam or not
1. get user input(id of student) and searching it in the array.
2. create a method with an if statement to verify if the student is able to take the exam or not
*/
s = searchStudent(studentList);
takeExam(s);
break;
case 5:
/* Calculating the letter grade for the student.
1. get user input (id of student) and search it in the array.
2. create a method to calculate the marks and give a letter grade of the student.
*/
s = searchStudent(studentList);
calculateMark(s);
break;
case 6:
/* save array of courses into a file
*/
saveStudent(studentList);
break;
case 7:
loadStudent(studentList);
break;
}
system("PAUSE");
}while(choice != 8);
return (0);
}
student createStudent(){
student s;
cout << "Enter the ID of the student." << endl;
getline(cin, s.id);
cout << "Enter the name of the student." << endl;
getline(cin, s.name);
cout << "Enter the attendance of the student. (Percentage eg.1-100)" << endl;
cin >> s.attendance;
cout << "Enter the assignment marks of the student. (Max 20)" << endl;
cin >> s.assignment;
cout << "Enter the quiz marks of the student. (Max 10)" << endl;
cin >> s.quiz;
cout << "Enter the midterm mark of the student. (Max 20)" << endl;
cin >> s.midterm;
cout << "Enter the practical exam mark of the student. (Max 30)" << endl;
cin >> s.practical;
cout << "Enter the final exam mark of the student. (Max 20)" << endl;
cin >> s.finalexam;
return(s);
}
void addStudent(student studentList[], student s){
/* adding student into an array
1. make a loop equal to the size of the array.
2. make a loop to check for emtpy space inside the array.
3. add information into the array.
*/
for(int i=0; i<20; i++){
// finds an empty array
if(studentList[i].id == "xxxxxx"){
// adds student into array
studentList[i] = s;
// stops the inputting of data to avoid all the arrays being repeated with same data
break;
}
}
}
student searchStudent(student studentList[]){
/* searching for a student by id
1. create variables to keep user input and returning the value.
2. get user input.
3. create a loop to search for the id inside the array.
*/
string id;
student s;
cout << "Enter the ID of the student." << endl;
getline (cin, id);
for( int i= 0; i<20; i++){
if (studentList[i].id == id){
s = studentList[i];
break;
}
}
return(s);
}
void removeStudent(student studentList[], student s){
/* removing data from an array
1. get information from searchStudent method.
2. search for student in array.
3. resetting array id to "xxxxxx"
*/
for (int i=0; i<20; i++){
if(studentList[i].id == s.id){
studentList[i].id = "xxxxxx";
break;
}
}
}
void showStudent(student s){
cout << "\n********************\n" << endl;
cout << "ID: " << s.id << endl;
cout << "Name: " << s.name << endl;
cout << "Attendance: " << s.attendance << endl;
cout << "Assignment: " << s.assignment << endl;
cout << "Quiz: " << s.quiz << endl;
cout << "Mid Term Exam: " << s.midterm << endl;
cout << "Practical Exam: " << s.practical << endl;
cout << "Final Exam: " << s.finalexam << endl;
cout << "\n********************\n" << endl;
}
void takeExam (student s){
if (s.attendance >= 80)
cout << "The student is able to take the final exam" << endl;
else
cout << "The student may not take the exam" << endl;
}
void calculateMark(student s){
/* calculating the score
1. add the input with type double from the array.
2. make an if/else statement for different numerical values into letter grades and display on to screen.
*/
double total;
total = s.assignment + s.finalexam + s.midterm + s.practical + s.quiz;
if (total > 100)
cout << "\n********************\n Your grade is : X \n********************\n" << endl;
else if (total >= 80)
cout << "\n********************\n Your grade is : A \n********************\n" << endl;
else if (total >= 70)
cout << "\n********************\n Your grade is : B \n********************\n" << endl;
else if (total >= 60)
cout << "\n********************\n Your grade is : C \n********************\n" << endl;
else if (total >= 50)
cout << "\n********************\n Your grade is : D \n********************\n" << endl;
else if (total >= 0)
cout << "\n********************\n Your grade is : F \n********************\n" << endl;
else
cout << "\n********************\n Your grade is : X \n********************\n" << endl;
}
void searchStudentName(student studentList[]){
// cannot break because it needs to find more
// instead of returning a course, make it return a course list(otherwise it can only do it once)
// to solve this, make the program pint out right away within the loop.
string name;
student s;
cout << "enter course name: " << endl;
getline(cin, name);
for (int i =0; i<20; i++){
if (studentList[i].name.find(name) != -1)
cout << studentList[i].name << endl;
cout << studentList[i].id << endl;
}
}
// save course to file
void saveStudent(student studentList[]){
// include fstream to save/load file (file stream)
ofstream outfile;
outfile.open("student.txt", ios::out);
//wring into a file make a loop
for (int i=0; i<20; i++){
if(studentList[i].id != "xxxxxx"){
outfile << studentList[i].id << "|";
outfile << studentList[i].name <<endl;
outfile << studentList[i].assignment << endl;
outfile << studentList[i].attendance << endl;
outfile << studentList[i].finalexam << endl;
outfile << studentList[i].midterm << endl;
outfile << studentList[i].practical << endl;
outfile << studentList[i].quiz << endl;
}
}
cout << "Save completed." << endl;
outfile.close();
}
void loadStudent(student studentList[]){
// in file = if[stream]
ifstream infile;
infile.open("student.txt", ios::in);
if (infile.is_open()){
// eof = end of file
while (!infile.eof()){
// load courses from a file
/*
1. read a record from the file
2. store the record in array
*/
student s;
// telling getline to read into s.id until it sees a |
getline(infile, s.id, '|');
getline(infile, s.name);
infile >> s.assignment;
infile >> s.attendance;
infile >> s.finalexam;
infile >> s.midterm;
infile >> s.practical;
infile >> s.quiz;
for(int i=0; i<20; i++){
if(studentList[i].id == "xxxxxx"){
studentList[i] = s;
break;
}
}
}
cout << "Load completed." << endl;
infile.close();
}else {
cout << "File not found." << endl;
}
}
#include <string>
#include <fstream>
using namespace std;
struct student{
string id;
string name;
double attendance;
double assignment;
double quiz;
double midterm;
double practical;
double finalexam;
};
// creates an array for student
student studentList[20];
// declaration of the fuctions
student createStudent(void);
void addStudent(student [], student);
student searchStudent(student[]);
void removeStudent(student[], student);
void showStudent(student);
void takeExam(student);
void calculateMark(student);
// loads and saves a file
void saveStudent(student[]);
void loadStudent(student[]);
int main (void){
// cleans the array before adding data into them
for (int i=0; i<20; i++){
studentList[i].id = "xxxxxx";
}
student s;
// creates a display menu for users
int choice;
do{
system("CLS");
cout << "[1] Add a student." << endl;
cout << "[2] Delete a student." << endl;
cout << "[3] Search for a student by ID." << endl;
cout << "[4] Check if the student is able to take the exam (by ID)." << endl;
cout << "[5] Determine the grade for the student (by ID)." << endl;
cout << "[6] Save student to file" << endl;
cout << "[7] Load student to file" << endl;
cout << "[8] Exit." << endl;
cout << "Enter your choice" << endl;
cin >> choice;
cin.ignore();
// creates a switch case
switch(choice){
case 1:
/* creating a student
1. asking user for information about the student.
2. saving the information into the array.
*/
s = createStudent();
addStudent(studentList, s);
break;
case 2:
/* deleting a student
1. ask users for the id of the student.
2. search for the id in the array.
3. removing the information
*/
s = searchStudent(studentList);
removeStudent(studentList, s);
break;
case 3:
/* searching for a student by id and displaying information
1. get user input (id) and searching it in the array.
2. create a method to display the information
*/
s = searchStudent(studentList);
showStudent(s);
break;
case 4:
/* Verifying if the student is able to take the exam or not
1. get user input(id of student) and searching it in the array.
2. create a method with an if statement to verify if the student is able to take the exam or not
*/
s = searchStudent(studentList);
takeExam(s);
break;
case 5:
/* Calculating the letter grade for the student.
1. get user input (id of student) and search it in the array.
2. create a method to calculate the marks and give a letter grade of the student.
*/
s = searchStudent(studentList);
calculateMark(s);
break;
case 6:
/* save array of courses into a file
*/
saveStudent(studentList);
break;
case 7:
loadStudent(studentList);
break;
}
system("PAUSE");
}while(choice != 8);
return (0);
}
student createStudent(){
student s;
cout << "Enter the ID of the student." << endl;
getline(cin, s.id);
cout << "Enter the name of the student." << endl;
getline(cin, s.name);
cout << "Enter the attendance of the student. (Percentage eg.1-100)" << endl;
cin >> s.attendance;
cout << "Enter the assignment marks of the student. (Max 20)" << endl;
cin >> s.assignment;
cout << "Enter the quiz marks of the student. (Max 10)" << endl;
cin >> s.quiz;
cout << "Enter the midterm mark of the student. (Max 20)" << endl;
cin >> s.midterm;
cout << "Enter the practical exam mark of the student. (Max 30)" << endl;
cin >> s.practical;
cout << "Enter the final exam mark of the student. (Max 20)" << endl;
cin >> s.finalexam;
return(s);
}
void addStudent(student studentList[], student s){
/* adding student into an array
1. make a loop equal to the size of the array.
2. make a loop to check for emtpy space inside the array.
3. add information into the array.
*/
for(int i=0; i<20; i++){
// finds an empty array
if(studentList[i].id == "xxxxxx"){
// adds student into array
studentList[i] = s;
// stops the inputting of data to avoid all the arrays being repeated with same data
break;
}
}
}
student searchStudent(student studentList[]){
/* searching for a student by id
1. create variables to keep user input and returning the value.
2. get user input.
3. create a loop to search for the id inside the array.
*/
string id;
student s;
cout << "Enter the ID of the student." << endl;
getline (cin, id);
for( int i= 0; i<20; i++){
if (studentList[i].id == id){
s = studentList[i];
break;
}
}
return(s);
}
void removeStudent(student studentList[], student s){
/* removing data from an array
1. get information from searchStudent method.
2. search for student in array.
3. resetting array id to "xxxxxx"
*/
for (int i=0; i<20; i++){
if(studentList[i].id == s.id){
studentList[i].id = "xxxxxx";
break;
}
}
}
void showStudent(student s){
cout << "\n********************\n" << endl;
cout << "ID: " << s.id << endl;
cout << "Name: " << s.name << endl;
cout << "Attendance: " << s.attendance << endl;
cout << "Assignment: " << s.assignment << endl;
cout << "Quiz: " << s.quiz << endl;
cout << "Mid Term Exam: " << s.midterm << endl;
cout << "Practical Exam: " << s.practical << endl;
cout << "Final Exam: " << s.finalexam << endl;
cout << "\n********************\n" << endl;
}
void takeExam (student s){
if (s.attendance >= 80)
cout << "The student is able to take the final exam" << endl;
else
cout << "The student may not take the exam" << endl;
}
void calculateMark(student s){
/* calculating the score
1. add the input with type double from the array.
2. make an if/else statement for different numerical values into letter grades and display on to screen.
*/
double total;
total = s.assignment + s.finalexam + s.midterm + s.practical + s.quiz;
if (total > 100)
cout << "\n********************\n Your grade is : X \n********************\n" << endl;
else if (total >= 80)
cout << "\n********************\n Your grade is : A \n********************\n" << endl;
else if (total >= 70)
cout << "\n********************\n Your grade is : B \n********************\n" << endl;
else if (total >= 60)
cout << "\n********************\n Your grade is : C \n********************\n" << endl;
else if (total >= 50)
cout << "\n********************\n Your grade is : D \n********************\n" << endl;
else if (total >= 0)
cout << "\n********************\n Your grade is : F \n********************\n" << endl;
else
cout << "\n********************\n Your grade is : X \n********************\n" << endl;
}
void searchStudentName(student studentList[]){
// cannot break because it needs to find more
// instead of returning a course, make it return a course list(otherwise it can only do it once)
// to solve this, make the program pint out right away within the loop.
string name;
student s;
cout << "enter course name: " << endl;
getline(cin, name);
for (int i =0; i<20; i++){
if (studentList[i].name.find(name) != -1)
cout << studentList[i].name << endl;
cout << studentList[i].id << endl;
}
}
// save course to file
void saveStudent(student studentList[]){
// include fstream to save/load file (file stream)
ofstream outfile;
outfile.open("student.txt", ios::out);
//wring into a file make a loop
for (int i=0; i<20; i++){
if(studentList[i].id != "xxxxxx"){
outfile << studentList[i].id << "|";
outfile << studentList[i].name <<endl;
outfile << studentList[i].assignment << endl;
outfile << studentList[i].attendance << endl;
outfile << studentList[i].finalexam << endl;
outfile << studentList[i].midterm << endl;
outfile << studentList[i].practical << endl;
outfile << studentList[i].quiz << endl;
}
}
cout << "Save completed." << endl;
outfile.close();
}
void loadStudent(student studentList[]){
// in file = if[stream]
ifstream infile;
infile.open("student.txt", ios::in);
if (infile.is_open()){
// eof = end of file
while (!infile.eof()){
// load courses from a file
/*
1. read a record from the file
2. store the record in array
*/
student s;
// telling getline to read into s.id until it sees a |
getline(infile, s.id, '|');
getline(infile, s.name);
infile >> s.assignment;
infile >> s.attendance;
infile >> s.finalexam;
infile >> s.midterm;
infile >> s.practical;
infile >> s.quiz;
for(int i=0; i<20; i++){
if(studentList[i].id == "xxxxxx"){
studentList[i] = s;
break;
}
}
}
cout << "Load completed." << endl;
infile.close();
}else {
cout << "File not found." << endl;
}
}