development of c++ program for storing and reading of information about a group of students

Upload: samson-oluwaseun-fadiya

Post on 05-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    1/22

    1

    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

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    2/22

    2

    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 diskfile. 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.

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    3/22

    3

    Chapter2 General Information about File operations in C++

    2.1 Data Flows

    Scores Students ID e.g. Students

    names Detail

    2.2 - Files and Operation in C++

    File processing in C++ is performed using the fstream class. Unlike

    theFILEstructure, 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++ providesthe ifstream class. The ifstream class provides various constructors youcan use, two of which are particularly important [2]. If you have enough

    Students

    Records

    1

    Check

    options/enter

    &

    Browse files contents

    http://www.functionx.com/cpp/articles/cfileprocessing.htmhttp://www.functionx.com/cpp/articles/cfileprocessing.htmhttp://www.functionx.com/cpp/articles/cfileprocessing.htmhttp://www.functionx.com/cpp/articles/cfileprocessing.htm
  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    4/22

    4

    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 anatural number that follows the table of modes as we described above.

    If necessary, you can also declare an empty instance of the ifstream classusing the default constructor:ifstream();

    After declaring this constructor, you can use the ifstream open() methodto 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 youwant to open a file; that is, you want to use the FileMode attribute with a

    value ofios in. Therefore, the second argument is already set to iosin 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 existingfile, you can make sure the file really exists, retrieve its name and pass it tothe fopen() function [1].

    Because the fopen() function is used to save a new file, to open an existingone, 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 toperform.

    The function seekg() will put the inside-pointer to a specific place (specifiedby 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

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    5/22

    5

    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 Description

    ios in Open file to read

    ios out Open file to write

    ios appAll the date you write, is put at the end of the file. It callsios::out

    ios ateAll the date you write, is put at the end of the file. It does notcall ios::out

    ios trunc Deletes all previous content in the file. (empties the file)

    ios nocreateIf the file does not exists, opening it with the open() function

    gets impossible.

    iosnoreplace

    If the file exists, trying to open it with the open() function,returns an error.

    ios binary Opens the file in binary mode.

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    6/22

    6

    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 studentsand 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 Enteron the keyboard and

    follow by the rest of other program options on the main menu.

    Figure1

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    7/22

    7

    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)

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    8/22

    8

    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:

    declares the objects used to communicate through the standard

    input and output (including cin and cout). 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. : 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 namesthat sound like yes/no questions in the program source text.

    #include#include#include#include#include

    /*using the name spaces in the header files, The C++ Standard providesnamespace facilities to provide greater control over the scope of names*/

    usingnamespace std;

    /*Void is a value of a function type that means nothing is returned.So, theaddress placed in a pointer must have the same type as the pointer. Variablesloops 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(){

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    9/22

    9

    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 loopdo{

    system("cls");cout

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    10/22

    10

    {return 0;

    }else{

    cout

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    11/22

    11

    inFile.open("info.csv");

    //add the details to filefor(int i=0;i

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    12/22

    12

    istringstream linestream(line);string item;int itemnum = 0;while (getline (linestream, item, ',')){

    x[linenum][itemnum] = item;itemnum++;

    }linenum++;}inFile.close();

    }}

    //allocates individual detailsvoid breakArray(string x[], string y[][7], int num){

    for(int i=0;i

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    13/22

    13

    "Name:\t\t" + y[i]

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    14/22

    14

    void sortArray(string e[][7]){

    string temp1,temp2,temp3,temp4,temp5,temp6,temp7;

    for(int i=0;i

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    15/22

    15

    int convertInt(string acc){

    int intReturn;intReturn = atoi(acc.c_str());

    return intReturn;}

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    16/22

    16

    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.

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    17/22

    17

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

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    18/22

    18

    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.

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    19/22

    19

    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 thecodes.

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    20/22

    20

    This options display the information about the system administrator.

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    21/22

    21

    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 diskand 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 . 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 simplybe 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.

  • 7/31/2019 Development of C++ Program for Storing and Reading of Information About a Group of Students

    22/22

    22

    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