100% found this document useful (1 vote)
30 views21 pages

Report Group 4 C++

The document describes a quiz system project that manages student and question information. The project consists of 6 mini projects including building a student database, listing and modifying questions, searching questions, and creating quizzes. The source code provided implements functions for these features.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
30 views21 pages

Report Group 4 C++

The document describes a quiz system project that manages student and question information. The project consists of 6 mini projects including building a student database, listing and modifying questions, searching questions, and creating quizzes. The source code provided implements functions for these features.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

INTERNATIONAL UNIVERSITY–VIETNAM NATIONAL UNIVERSITY

DEPARTMENT OF INDUSTRIAL AND SYSTEMS ENGINEERING

COURSE: Intro to Programming -C++/C#

PROJECT: QUIZ SYSTEM

Lecturer: Dr. Do Vinh Truc


Group Member:
Lương Thị Ngọc Ánh IELSIU17064

Phan Vũ Nhật Linh IELSIU17115

Lê Phạm Hiếu Ngân IELSIU17077

Nguyễn Hoàng Thu Uyên IELSIU17103

Nguyễn Văn Duy IELSIU17057


I. Introduction:

1. Overview:
The main objective of the C + + Project on Quiz System is to manage the details of
Students, Results, Marks manages all the information about Students, Papers, Marks,
Students. The project is totally built at administrative end and thus only the
administrator guaranteed the access. The purpose of the project is to build an
application program to reduce the manual work for managing the Students, Papers.
2. Description:
The Quiz System consists of 6 mini projects which are solved by the solution given
below. Quizzes consisting of multiple-choice questions, are a common way to assess
the performance of students in a class. This simple electronic quiz system is to make
quiz taking easier and the scoring faster.
Mini project 1: Build user account database stored in the “student.txt” which input
from GUI or console interface and login function. The function of this program is to
distinguish between student and lecturer.
Mini project 2: List all questions and add new question into the list.
Mini project 3: List all questions and modify an existing question.
Mini project 4: Search for a question from test bank using a phrase and remove it from
the database.
Mini project 5: List all questions in the database and add them into new a new quiz.
Mini project 6: Students answer a quiz. The results are stored in solutions.txt file.
3. Function:
Functionalities provided by C + + Project on Quiz System are as follows:
• Provides the searching facilities based on various factors.
• Tracks all the information of Papers, Results ect
• Manage the information of Lecturers
• Shows the information and description of the Students to increase efficiency of
managing the Students.
• It deals with monitoring the information and transactions of Results.
• Manage the information of Students
• Editing, adding and updating of Records is improved which results in proper
resource management of Students data.
• Integration of all records of Marks.
4. Application:
The project will do a big help for students and lecturers. Students can answer the
quizzes and submit them by their very names, while lecturers can list, add new, modify,
search for, remove questions or add into a new quiz.
II. Flowchart

III. Source code


1. QuizSystem.h
Code:
#pragma once
#include<string>
#include<vector>
#include<ctime>
#include<time.h>
#include<fstream>
#include<iostream>
using namespace std;

struct student{
int accountID;
string name, password, role;
bool isSubmitted = 0;
};

struct question{
int questionsID;
string desc;
int correct;
};

struct quiz{
int quizID;
vector<int> questionID;
int dayDeadline, monthDeadline, yearDeadline;
};

void readAccFromFile (string inFile, vector<student>& arrStu);


student createAcc (ofstream& fo, vector<student> &arrStu);
void writeAcc (ofstream& fo, student st);
void writeAllQuesToFile (string outFile, vector<question> arrQues);
student login (vector<student> arr);

void printQues (ostream& os, question ques);


void showAllQuestions (vector<question> arrQues);
question readOneQuestionFromFile (ifstream& fi);
void readQuestionFromFile (ifstream& fi, vector<question>& arrQues);
question readOneQuestionFromConsole ();
void addQuestionFromFile (string inFile, vector<question> &arrQues, string outFile);
void addQuestionsFromConsole (vector<question> &arrQues, string outFile);

void modifyQuestionsFromFile (vector<question>& arrQues, string outFile);


void modifyQuestionsFromConsole (vector<question> &arrQues, string outFile);
void searchQuestions (vector<question> &arrQues, string outFile);
void deleteQuestion (vector<question> &arrQues, int questionID, string outFile);
void readQuiz (string inFile, vector<quiz> &arrQuiz);
void createQuiz (vector<question> arrQues, vector<quiz> &arrQuiz, vector<student> arrStudent,
string outFile);
void test (vector<quiz> arrQuiz, vector<question> arrQues, student& student, ofstream& fo);
string upcase (string str);

2. QuizSystem.cpp
a. Code:
#include "QuizSystem.h"

void readAccFromFile (string inFile, vector<student>& arrStu){


ifstream fi (inFile);
student stuTemp;
string tmp;

while(!fi.eof ()){
getline (fi, tmp, ' ');
getline (fi, tmp);
stuTemp.accountID = stoi (tmp);

getline (fi, tmp, ' ');


getline (fi, stuTemp.name);

getline (fi, tmp, ' ');


getline (fi, stuTemp.password);

getline (fi, tmp, ' ');


getline (fi, stuTemp.role);

getline (fi, tmp);


getline (fi, tmp);

arrStu.push_back (stuTemp);
}
fi.close ();
}

student createAcc (ofstream& fo, vector<student>& arr){


student tmp;
tmp.accountID = arr.size () + 1;
cout << "Account ID= " << tmp.accountID << endl;
cout << "Name: ";
cin.ignore (32767, '\n');
getline (cin, tmp.name);
cout << "Password: ";
getline (cin, tmp.password);
cout << "Role: ";
getline (cin, tmp.role);
arr.push_back (tmp);
writeAcc (fo, tmp);
return tmp;
}

void writeAcc (ofstream& fo, student st){


fo << "\n\nAccountID: " << st.accountID << endl;
fo << "Name: " << st.name << endl;
fo << "Password: " << st.password << endl;
fo << "Role: " << st.role << endl;
}

void writeAllQuesToFile (string outFile, vector<question> arrQues){


ofstream fo (outFile);
for(int i = 0; i < arrQues.size (); i++){
fo << "QuestionID: " << arrQues[i].questionsID << endl;
fo << arrQues[i].desc;
fo << "Correct: " << arrQues[i].correct << endl << endl;
}
fo.close ();
}

student login (vector<student> arr){


int id, k = -1;
string pass;
cout << "Enter ID va pass\n";
do{
cout << "ID: ";
cin >> id;
for(int i = 0; i <= arr.size (); i++)
if(arr[i].accountID == id){
k = i;
break;
}
if(k != -1){
break;
} else cout << "Wrong ID\n\n";
} while(1);

do{
cout << "Password: ";
cin >> pass;
if(arr[k].password == pass)
return arr[k];
else{
cout << "Wrong password!!!\n";
}
} while(1);
}

void printQues (ostream& os, question ques){


os << "QuestionID: ";
os << ques.questionsID << endl;
os << ques.desc;
os << "Correct: ";
os << ques.correct << "\n\n";
}

void showAllQuestions (vector<question> arrQues){


for(int i = 0; i < arrQues.size (); i++){
printQues (cout, arrQues[i]);
cout << endl;
}
}

question readOneQuestionFromFile (ifstream& fi){


question ques;
string tmp, desc, correct;
desc = "";
int i = 0;

do{
getline (fi, tmp);
desc = desc + tmp + "\n";
i++;
} while(i <= 4);
ques.desc = desc;

getline (fi, tmp, ' ');


getline (fi, correct);
ques.correct = stoi (correct);
return ques;
}

void readQuestionFromFile (ifstream & fi, vector<question> & arrQues){


while(!fi.eof ()){
question quesTMP;
string tmp, desc;
char chr;
getline (fi, tmp, ' ');
getline (fi, tmp);
quesTMP.questionsID = stoi (tmp);
desc = "";
int i = 0;
do{
getline (fi, tmp);
desc = desc + tmp + "\n";
i++;
} while(i <= 4);
quesTMP.desc = desc;

getline (fi, tmp, ' ');


getline (fi, tmp);
quesTMP.correct = stoi (tmp);

arrQues.push_back (quesTMP);

fi >> chr;
while(!fi.eof () && chr == '\n'){
getline (fi, tmp);
}
}
}

question readOneQuestionFromConsole (){


question ques;
string str;
ques.desc = "";
cout << "Desc: ";
cin.ignore (32767, '\n');
getline (cin, str);
ques.desc = "Desc: " + str + '\n';

cout << "Sol1: ";


getline (cin, str);
ques.desc = ques.desc + "Sol1: " + str + '\n';

cout << "Sol2: ";


getline (cin, str);
ques.desc = ques.desc + "Sol2: " + str + '\n';

cout << "Sol3: ";


getline (cin, str);
ques.desc = ques.desc + "Sol3: " + str + '\n';

cout << "Sol4: ";


getline (cin, str);
ques.desc = ques.desc + "Sol4: " + str + '\n';
cout << "Correct: ";
cin >> ques.correct;

return ques;
}

void addQuestionFromFile (string inFile, vector<question> & arrQues, string outFile){


ifstream fi (inFile);
if(!fi){
cout << "Error!";
return;
}
while(!fi.eof ()){
string tmp;
question quesTMP = readOneQuestionFromFile (fi);
quesTMP.questionsID = arrQues.size ();
arrQues.push_back (quesTMP);

getline (fi, tmp);


while(!fi.eof () && tmp == "\n"){
getline (fi, tmp);
}
}
writeAllQuesToFile (outFile, arrQues);
cout << "Add question from file " << inFile << " Successful!\n";
fi.close ();
}

void addQuestionsFromConsole (vector<question> & arrQues, string outFile){


int tmp = 0;
do{
question quesTMP = readOneQuestionFromConsole ();
quesTMP.questionsID = arrQues.size () + 1;
arrQues.push_back (quesTMP);
cout << "0.Continue to add question\n1.Stop adding question\n";
cin >> tmp;
} while(tmp == 0);
writeAllQuesToFile (outFile, arrQues);
cout << "Add question successfully!!\n";
}

void modifyQuestionsFromFile (vector<question> & arrQues, string outFile){


int id;
cout << "Enter QuestionID : ";
cin >> id;
string inFile;
cout << "Enter file input: ";
cin >> inFile;

ifstream fi (inFile);
question tmpQues = readOneQuestionFromFile (fi);

int i = 0;
for(i; i < arrQues.size (); i++)
if(arrQues[i].questionsID == id) break;
arrQues[i].desc = tmpQues.desc;
arrQues[i].correct = tmpQues.correct;
writeAllQuesToFile (outFile, arrQues);
cout << " Modify question successfully\n";
fi.close ();
}

void modifyQuestionsFromConsole (vector<question> & arrQues, string outFile){


int id;
cout << "Enter QuestionID: ";
cin >> id;

question tmpQues = readOneQuestionFromConsole ();


int i = 0;
for(i; i < arrQues.size (); i++)
if(arrQues[i].questionsID == id) break;
arrQues[i].desc = tmpQues.desc;
arrQues[i].correct = tmpQues.correct;

writeAllQuesToFile (outFile, arrQues);


cout << " Modify question successfully\n";
}

void searchQuestions (vector<question> & arrQues, string outFile){


system ("CLS");
string strSearch;
cout << "Enter the string searched: ";
cin >> strSearch;
int pos = -1;
int i = 0;
for(i; i < arrQues.size (); i++){
pos = arrQues[i].desc.find (strSearch, 0);
if(pos != -1) break;
}
if(pos == -1){
cout << "Not found!\n";
cout << "0.Exit\n";
cout << "1.Find again\n";
cout << " Please enter: ";
int k;
cin.ignore (32767, '\n');
cin >> k;
if(k == 1) searchQuestions (arrQues, outFile);
else return;
}
else{
int k;
cout << "Find\n";
cout << "1.Show the question\n";
cout << "2.Delete question\n";
cout << " Please enter: ";

cin.ignore (32767, '\n');


cin >> k;
cout << endl;
if(k == 1)
printQues (cout, arrQues[i]);
else if(k == 2)
deleteQuestion (arrQues, i, outFile);
else
cout << "Wrongly entered! Exit\n";
}
}

void deleteQuestion (vector<question> & arrQues, int questionID, string outFile){


int i = -1;
do{
i++;
if(arrQues[i].questionsID == questionID) break;
} while(true);
if(i == -1){
cout << "Not found question\n";
return;
}
arrQues.erase (arrQues.begin () + i);
writeAllQuesToFile (outFile, arrQues);
cout << "Delete question successfully\n";
}

void readQuiz (string inFile, vector<quiz> & arrQuiz){


ifstream fi (inFile);
quiz tmpQuiz;
string tmp;
char chr;
while(!fi.eof ()){
getline (fi, tmp, ' ');
getline (fi, tmp);
tmpQuiz.quizID = stoi (tmp);

getline (fi, tmp, ' ');


for(int i = 0; i < 4; i++){
getline (fi, tmp, ' ');
tmp.erase (tmp.length () - 1, 1);
tmpQuiz.questionID.push_back (stoi (tmp));
}
getline (fi, tmp);
tmpQuiz.questionID.push_back (stoi (tmp));

getline (fi, tmp, ' ');


getline (fi, tmp, '/');
tmpQuiz.dayDeadline = stoi (tmp);

getline (fi, tmp, '/');


tmpQuiz.monthDeadline = stoi (tmp);

getline (fi, tmp);


tmpQuiz.yearDeadline = stoi (tmp);

arrQuiz.push_back (tmpQuiz);
fi >> chr;
while(!fi.eof () && chr == '\n'){
getline (fi, tmp);
}
}
fi.close ();
}

void createQuiz (vector<question> arrQues, vector<quiz> & arrQuiz, vector<student> arrStudent,


string outFile){
ofstream fo (outFile);
int day, month, year;
cout << "Enter deadline quiz:\n";
cout << "Day: ";
cin >> day;
cout << "Month: ";
cin >> month;
cout << "Year: ";
cin >> year;
cout << endl;

quiz temp;
temp.dayDeadline = day;
temp.monthDeadline = month;
temp.yearDeadline = year;

arrQuiz.resize (0);
srand (time (NULL));

for(int i = 0; i < arrStudent.size (); i++){


if(upcase (arrStudent[i].role) == "STUDENT"){

temp.quizID = arrStudent[i].accountID;

cout << "QuizID: " << temp.quizID << endl;

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


temp.questionID.resize (5);
int r= rand ();
temp.questionID[i] = r % arrQues.size ();
printQues (cout, arrQues[temp.questionID[i]]);

}
arrQuiz.push_back (temp);
cout << "\n\n";

fo << "QuizID: " << temp.quizID << endl;


fo << "QuestionIDs: ";
for(int i = 0; i < 4; i++){
fo << temp.questionID[i] << ", ";
}
fo << temp.questionID[4] << endl;
fo << "Deadline: " << day << "/" << month << "/" << year << "\n\n";
}
}
cout << "Create quiz successfully\n";
fo.close ();
}

void test (vector<quiz> arrQuiz, vector<question> arrQues, student & stu, ofstream & fo){
quiz QUIZ;

bool check = 0;
for(int i = 0; i < arrQuiz.size (); i++)
if(arrQuiz[i].quizID == stu.accountID){
QUIZ = arrQuiz[i];
check = 1;
break;
}
if(!check){
cout << "No quiz for this student\n";
return;
}

if(stu.isSubmitted){
cout << "Did the quiz!\n";
return;
}

time_t now;
struct tm deadline;
time (&now); /* get current time; same as: now = time(NULL) */

deadline = *localtime (&now);

deadline.tm_hour = 0; deadline.tm_min = 0; deadline.tm_sec = 0;


deadline.tm_mon = QUIZ.monthDeadline - 1; deadline.tm_mday = QUIZ.dayDeadline;
deadline.tm_year = QUIZ.yearDeadline - 1900;

double seconds = difftime (mktime (&deadline), now);

vector<string> ans;
ans.resize (5);
if(seconds <= 0){
cout << "Late deadline\n";
return;
} else{
for(int i = 0; i < QUIZ.questionID.size (); i++){
for(int k = 0; k < arrQues.size (); k++)
if(arrQues[k].questionsID == QUIZ.questionID[i]){
cout << arrQues[k].desc;
break;
}
cout << "Correct: ";
cin >> ans[i];
}
stu.isSubmitted = 1;
}

tm* ltm = localtime (&now);


fo << "SolutionID: " << stu.accountID << endl;
fo << "StudentID: " << stu.accountID << endl;
fo << "SubmissionDate: ";
fo << ltm->tm_mday << "/";
fo << 1 + ltm->tm_mon << "/";
fo << 1900 + ltm->tm_year << "\n";
fo << "Content: ";
for(int i = 0; i < 4; i++)
fo << QUIZ.questionID[i] << "-" << ans[i] << ", ";
fo << QUIZ.questionID[4] << "-" << ans[4] << "\n";
}

string upcase (string str){


string tmp = str;
for(int i = 0; i < str.length (); i++)
tmp[i] = toupper (tmp[i]);
return tmp;
}
b. Components :
• Read account from file ( line 3-29)
• Create a new account from console ( line 31-45 )
• Write account ( line 47-52)
• Write all questions to file ( line 54-62)
• Login from console and check the information with student.txt ( line 65-91)
• Print Questions ( line 93-99)
• Show all questions ( line 101-188)
• Add new questions from File ( line 190-210)
• Add new questions from Console ( line 213-224)
• Modify questions from File ( line 226-246)
• Modify question from Console( line 248-262)
• Search question from test bank (line 265-305)
• Remove question from database (line 307-320)
• Create a new quiz from question.txt ( line 322-410)
• Student answer the quiz from quizzes.txt and write solution to solutions.txt (line 413-482)

3. Main.cpp
a. Code:
#include"QuizSystem.h"
using namespace std;

string studentList = "student.txt";


string questionList = "questions.txt";
string quizList = "quizzes.txt";
string studentSolution = "solutions.txt";
vector<student> arrStudent;
vector<question> arrQuestion;
vector<quiz> arrQuiz;
int main (){

ofstream fileStudent, fileSolution;


ifstream fileQuestion;
fileStudent.open (studentList, ios::app);
fileSolution.open (studentSolution, ios::app);
fileQuestion.open (questionList);

readQuiz (quizList, arrQuiz);


readAccFromFile (studentList, arrStudent);
readQuestionFromFile (fileQuestion, arrQuestion);
fileQuestion.close ();

int mode;
student account;
system ("CLS");
do{
cout << "1.Login\n2.Create account\n";
cout << "Enter the function: ";
cin >> mode;
if(mode == 1){
account = login (arrStudent);
if(upcase (account.role) == "STUDENT"){
do{
system ("CLS");
cout << "Login successfully\n Enter 1 to answer the quiz: ";
cin >> mode;
if(mode == 1){
test (arrQuiz, arrQuestion, account, fileSolution);
break;
}
} while(1);
} else if(upcase (account.role) == "LECTURER"){
do{
system ("CLS");
cout << "\n1.Show all questions\n";
cout << "2.Add new question from file\n";
cout << "3.Add new question from console\n";
cout << "4.Modify question from file\n";
cout << "5.Modify question from console\n";
cout << "6.Search question\n";
cout << "7.Delete question\n";
cout << "8.Create quiz\n";
cout << "Enter the function: ";
cin >> mode;
cout << endl;
string inFile;
switch(mode){
case 1:
showAllQuestions (arrQuestion);
break;
case 2:
cout << "Enter file input: ";
cin.ignore (32767, '\n');
cin >> inFile;
addQuestionFromFile (inFile, arrQuestion, questionList);
break;
case 3:
addQuestionsFromConsole (arrQuestion, questionList);
break;
case 4:
modifyQuestionsFromFile (arrQuestion, questionList);
break;
case 5:
modifyQuestionsFromConsole (arrQuestion, questionList);
break;
case 6:
searchQuestions (arrQuestion, questionList);
break;
case 7:
cout << "Enter QuestionID: ";
int id;
cin >> id;
deleteQuestion (arrQuestion, id, questionList);
break;
case 8:
createQuiz (arrQuestion, arrQuiz, arrStudent, quizList);
break;
default:
break;
}
cout << "1.Continue to execute the function\n0.Logout\n";
cout << "Enter the function: ";
cin >> mode;
if(mode == 1) continue;
else break;
} while(true);

}
} else if(mode == 2){
account = createAcc (fileStudent, arrStudent);
break;
}
system ("CLS");
cout << "1.Login\\Create account\n0.Exit\n";
cout << "Enter the function: ";
cin >> mode;
if(mode != 1) break;
} while(true);

fileStudent.close ();
fileSolution.close ();
return 0;
}
b. Components:
• Declare variables (line 4-10)
• Open all the file (line 14-23)
• Check account which is a student or lecturer (line 25-44)
• Table of cases of lecturer (line 45-93)
• Choose the next step (continue or logout) (line 94-101)
• Create the account (line 102-105)
• Choose the next step (continue or exit) (line 106-111)
• Close the file (line 113-115)

IV. Result:
V. References

https://www.freeprojectz.com/c-projects-projects/c-project-multiple-choice-quiz-system

http://cppprojectcode.blogspot.com/2010/09/quiz-test.html

You might also like