You are on page 1of 22

NAME: SAMSON OLUWASEUN FADIYA

DEPARTMENT: MANAGEMENT INFORMATION SYSTEM

COURSE CODE & NAME: COSC 511 ADVANCED PROGRAMMING LANGUAGES

TITLE: Development of C++ program for storing and reading of information about a group of students.

SUBMITTED TO: PROFESSOR ALEXANDER KOSTIN

Chapter1 - Introduction
A program typically kinds of data communication: Data transfer between the console unit and the program. Data transfer between the program and a disk file. The input/output system of C++ handles file operations which are very much similar to the console input and output operations. It uses file streams as an interface between the programs and the files. The stream that supplies data to the program is known as input stream and the one that receives data from the program is known as output stream. In other words, the input stream extracts or reads data from the file and the output stream inserts or writes data to the file. The purpose of this project is to develop a C++ program for storing and reading of information about a group of students.

Chapter2 General Information about File operations in C++


2.1 Data Flows

1 Students Check options/enter Students

Records

Scores names

&
Browse files contents

ID e.g. Students Detail

2.2 - Files and Operation in C++ File processing in C++ is performed using the fstream class. Unlike the FILE structure, fstream is a complete C++ class with constructors, a destructor and overloaded operators [2]. To perform file processing, you can declare an instance of an fstream object. If you do not yet know the name of the file you want to process, you can use the default constructor. Unlike the FILE structure, the fstream class provides two distinct classes for file processing. One is used to write to a file and the other is used to read from a file. 2.3 - How to Open a File Another operation you can perform consists of opening an already existing file to have access to its contents. To do this, C++ provides the ifstream class. The ifstream class provides various constructors you can use, two of which are particularly important [2]. If you have enough

information about the file you want to open, you can use the following constructor: ifstream (const char* FileName, int FileMode); The first argument of the constructor, FileName, is a constant string that represents the file that you want to open. The FileMode argument is a natural number that follows the table of modes as we described above. If necessary, you can also declare an empty instance of the ifstream class using the default constructor: ifstream(); After declaring this constructor, you can use the ifstream open() method to formally open the intended file. The syntax of the open() method is: open(const char* FileName, int FileMode); This method uses the same arguments as the above constructor. By default, when declaring an instance of the ifstream class, it is assumed that you want to open a file; that is, you want to use the FileMode attribute with a value of ios in. Therefore, the second argument is already set to ios in value. This allows you to call the open() method [1].

2.4 - How to Create a File To create a new file, open an existing file, or save a file, you use the fopen() function. Its syntax is: FILE *fopen(const char *FileName, const char *Mode); The first argument, FileName, must be a valid name of a file. If the user is creating or saving a new file, you can let him specify the name of the file, following the rules of the operating system. If the user is opening an existing file, you can make sure the file really exists, retrieve its name and pass it to the fopen() function [1]. Because the fopen() function is used to save a new file, to open an existing one, or to save a file that was only modified, the second argument, Mode, actually allows you to decide what operation the function will be used to perform. The function seekg() will put the inside-pointer to a specific place (specified by you). You can use: ios beg - to put it in the beginning of the file ios end - to put it at the end of the file

or you can also set the number of characters to go back or after [3]. For example, if you want to go 5 characters back, you should write: File.seekg(-5);

2.5 Typical Operations According to Table1 below, when processing a file, you will typically specify the type of operation you want to perform. The operation is specified using what is referred to as a file mode. It can be one of the following: Table1: List of main file operations Name ios in ios out ios app ios ate ios trunc ios nocreate ios noreplace ios binary Open file to read Open file to write All the date you write, is put at the end of the file. It calls ios::out All the date you write, is put at the end of the file. It does not call ios::out Deletes all previous content in the file. (empties the file) If the file does not exists, opening it with the open() function gets impossible. If the file exists, trying to open it with the open() function, returns an error. Opens the file in binary mode. Description

CHAPTER3 - Source Text & Explanations


3.1 Purpose and user interface of the program The purpose of this program, it will ask from the user each group of students and their basic information e.g. student number, name, groupno, GPA, faculty, department and date of entry. The screen shot below figure1; shows the main menu of the program on the command line interface, which display 5 option for the user to choose from, the option one, prompt the user to enter detail of the student as they comes one after the other by the press of command Enter on the keyboard and follow by the rest of other program options on the main menu. Figure1

3.2 Flowchart of the program and its description This flowchart diagram simply shows the step-by-step nature of a process, complete with the decision making that the process might involve. Figure2; (Using smart draw software)

3.3 Source text and its explanation The structure of the program is shown in figure2. this program source text is well detailed, The library and its hierarchy of classes is split in different files: <iostream> declares the objects used to communicate through the standard input and output (including cin and cout). <fstream> defines the file stream classes (like the template basic_ifstream or the class ofstream) as well as the internal buffer objects used with these (basic_filebuf). These classes are used to manipulate files using streams. <sstream>: The classes defined in this file are used to manipulate string objects as if they were streams. Using function like void and bool to structure our programs in a more modular way will be necessary, since void point to any variable that is not declared with the const keyword while bool give boolean functions names that sound like yes/no questions in the program source text.
#include #include #include #include #include <iostream> <sstream> <fstream> <string> <stdlib.h>

/*using the name spaces in the header files, The C++ Standard provides namespace facilities to provide greater control over the scope of names*/ using namespace std; /* Void is a value of a function type that means nothing is returned.So, the address placed in a pointer must have the same type as the pointer. Variables loops repeat until a certain condition is met. */ bool checkIsFile(); void addUser(string e[][7], int num); void displayUsers(string e[][7], int num); void deleteUser(string e[][7], int num); void readFromFile(string x[][7]); void breakArray(string x[], string y[][7], int num); bool studentExists(string x[], int y, string search); void findUser(string x[], string y[],string z[], string a[],string t[], string c[], string d[]); void sortArray(string e[][7]); int findRowNumber(); int convertInt(string acc); char cont(); int main() {

int option; int acc; char c; string stu[1000][7]; string student[1000], groupno[1000],name[1000], faculty[1000], department[1000], gpa[1000], dateintake[1000]; //infinite loop do{ system("cls"); cout<<"-------------------------\n"; cout<<"Menu Options\n"; cout<<"1 Add Student Number\n"; cout<<"2 List Student Info\n"; cout<<"3 Delete Info\n"; cout<<"4 Find user\n"; cout<<"5 Exit program\n"; cout<<"-------------------------\n"; cin>>option; if(option==1) { readFromFile(stu); addUser(stu, findRowNumber()); c = cont(); } else if(option==2) { readFromFile(stu); displayUsers(stu, findRowNumber()); c = cont(); } else if(option==3) { //delete a user; readFromFile(stu); deleteUser(stu, findRowNumber()); c = cont(); } else if(option==4) { //find user readFromFile(stu); breakArray(student,stu,0); breakArray(name,stu,1); breakArray(groupno,stu,2); breakArray(gpa,stu,3); breakArray(faculty,stu,4); breakArray(department,stu,5); breakArray(dateintake,stu,6); findUser(student,name,groupno,gpa,faculty,department,dateintake); c = cont(); } else if(option==5)

{ return 0; } else { cout<<"Invalid option\n"; } }while(c == 'y' || c == 'Y'); } //continue program char cont() { char c; cout<<"Do you wish to continue <Y/y>: "; cin>>c; return c; } //add data to file void addUser(string e[][7], int num) { system("cls"); string stu[19][7]; string acc[19]; string check; for(int a=0;a<num;a++) { for(int b=0;b<7;b++) { stu[a][b] = e[a][b]; } } breakArray(acc,stu,0); //just to test if it works cout<<"Enter Student Number: "; cin>> check; if(!studentExists(acc,num + 1, check)) { stu[num][0] = check; cout<<"Enter Name: "; cin>> stu[num][1]; cout<<"Enter GroupNo: "; cin>> stu[num][2]; cout<<"Enter GPA: "; cin>> stu[num][3]; cout<<"Enter Faculty: "; cin>> stu[num][4]; cout<<"Enter Department: "; cin>> stu[num][5]; cout<<"Enter DateofEntry: "; cin>> stu[num][6]; ofstream inFile;

10

inFile.open("info.csv"); //add the details to file for(int i=0;i<num +1; i++) { inFile << stu[i][0] + "," + stu[i][1] + "," + stu[i][2] + "," + stu[i][3] + "," + stu[i][4] + "," + stu[i][5] + "," + stu[i][6]<<endl; } inFile.close(); cout<<endl<<"Student Number: " + check + " added!"<<endl; }else { cout<<endl<<"Student Number already exists"<<endl; } } //displays all the data from file void displayUsers(string e[][7], int num) { if(findRowNumber() > 0) { sortArray(e); cout<<endl<<"ID\tName\tGNo\tGPA\tFaculty.\tDep.\tDEntry"<<endl; cout<<"-------------------------------------------------------------"<<endl; for(int a=0;a<num;a++) { cout<<e[a][0] + "\t" + e[a][1] + "\t" + e[a][2] + "\t" + e[a][3] + "\t" + e[a][4] + "\t" + e[a][5] + "\t" + e[a][6]<<endl; } cout<<endl; } } //retrieves data from file void readFromFile(string x[][7]) { ifstream inFile("info.csv"); if(!inFile) { cout<<"File not found- Create one by adding a student info!"<<endl<<endl; }else if(findRowNumber()==0) { cout<<"There's no student info on file- Create one by adding a student info!"<<endl<<endl; }else { string line; int linenum = 0; while (getline (inFile, line)) {

11

istringstream linestream(line); string item; int itemnum = 0; while (getline (linestream, item, ',')) { x[linenum][itemnum] = item; itemnum++; } linenum++; } inFile.close(); } } //allocates individual details void breakArray(string x[], string y[][7], int num) { for(int i=0;i<findRowNumber();i++) { for(int j=0;j<7;j++) { x[i] = y[i][num]; } } } //check if account exists bool studentExists(string x[], int y, string acc) { bool check = false; for(int i=0;i<y;i++) { if(x[i]==acc) { check = true; } } return check; } //find a user void findUser(string x[], string y[],string z[], string a[], string t[], string c[], string d[]) { string search; bool check = false; if(findRowNumber() > 0) { cout<<"Enter Student Number to Search: "; cin>>search; for(int i=0;i<findRowNumber();i++) { if(x[i]==search) { cout<<endl<<"Student Number: " + x[i]<<endl<<

12

"Name:\t\t" + y[i]<<endl<< "GNo:\t\t" + z[i]<<endl<< "GPA:\t\t" + a[i]<<endl<< "Fac:\t\t" + t[i]<<endl<< "Dep:\t\t" + c[i]<<endl<< "DEntry:\t\t" + d[i]<<endl<<endl; check = true; } } if(!check) cout<<endl<<"User does not exist"<<endl<<endl; } } //deletes user void deleteUser(string e[][7], int num) { if(findRowNumber() > 0 ) { string acc[7]; string stu[7][7]; breakArray(acc,e,0); string del; cout<<endl<<"Enter Student Number to Delete: "; cin>>del; if(studentExists(acc,num,del)) { ofstream temp; temp.open("temp.csv"); //re-add the details to file for(int i=0;i<num;i++) { if(e[i][0] != del) { temp << e[i][0] + "," + e[i][1] + "," + e[i][2] + "," + e[i][3] + "," + e[i][4] + "," + e[i][5] + "," + e[i][6]<<endl; } } temp.close(); remove("info.csv"); rename("temp.csv", "info.csv"); cout<<endl<<"Student Info: " + del + " deleted!"<<endl<<endl; }else { cout<<endl<<"Student Info does not exists!"<<endl<<endl; } } }

13

void sortArray(string e[][7]) { string temp1,temp2,temp3,temp4,temp5,temp6,temp7; for(int i=0;i<findRowNumber()-1;i++) { for(int j=0;j<findRowNumber()-1-i;j++) { if(convertInt(e[j + 1][0]) < convertInt(e[j][0])) { temp1 = e[j + 1][0]; e[j + 1][0] = e[j][0]; e[j][0] = temp1; temp2 = e[j + 1][1]; e[j + 1][1] = e[j][1]; e[j][1] = temp2; temp3 = e[j + 1][2]; e[j + 1][2] = e[j][2]; e[j][2] = temp3; temp4 = e[j + 1][3]; e[j + 1][3] = e[j][3]; e[j][3] = temp4; temp5 = e[j + 1][4]; e[j + 1][4] = e[j][4]; e[j][4] = temp5; temp6 = e[j + 1][5]; e[j + 1][5] = e[j][5]; e[j][5] = temp6; temp7 = e[j + 1][6]; e[j + 1][6] = e[j][6]; e[j][6] = temp7; } } } } //return number of items in file int findRowNumber() { ifstream inFile("info.csv"); string line; int linenum = 0; while (getline (inFile, line)) { linenum++; } inFile.close(); return linenum; } //converts account number from string to integer

14

int convertInt(string acc) { int intReturn; intReturn = atoi(acc.c_str()); return intReturn; }

15

Chapter 4 - Implementation The screen shots capture from the programs, following the running codes in each stages of testing;

Main menu with the options which prompt the user to the exact location of work to do.

16

The first option is choosen and prompt the user to enter the student details.

17

This option allows the user to browse through the contents store on the bufferstream and prompt the user to go back to the main menu to select more options.

18

This option search by the students Id, if not available it prompt the user to go back to the main menu by issuing a certain command embedded in the codes.

19

This options display the information about the system administrator.

20

Chapter 5 - Conclusion In conclusion, file handling is an important part of all programs. Most of the applications will have their own features to save some data to the local disk and read data from the disk again. C++ File I/O classes simplify such file read/write operations for the programmer by providing easier to use classes. File input and output are typically achieved by using an object of one of the classes: ifstream for reading input only, ofstream for writing output only and fstream for reading and writing from/to one file. All three classes are defined in <fstream.h>. Throughout this page, the term "file stream" will be used when referring to features that apply equally to all three classes mentioned. The file stream classes are designed with the idea that a file should simply be viewed as a stream or array of uninterrupted bytes. For convenience, the "array" of bytes stored in a file, where it create and manage the memory and the address of that memory when pass in parameters. The above example shows the use of a local variable to hold the results returned.

21

Reference
[1] Bjarne Stroustrup (December 25, 2008). Programming: Principles and Practice Using C++ . USA: Addison-Wesley Professional. 1272.

[2] Ray Lischner (April 1, 2003). C++ in a Nutshell . London: O'Reilly Media.704. [3] Stephen Prata (November 25, 2004). C++ Primer Plus . 5th ed. London: Sams. 1224. [4] cplusplus. (2000-2010). IOstream Library. Available: http://www.cplusplus.com/reference/iostream/. Last accessed 5th Jan

22

You might also like