The following tutorial will delve into datasets and databases. It is helpful that you look at the previous tutorials before going into this one.
To start this program off, we will need to call some modules. This time we will not just be calling the 'iostream' module, but we will also be calling the 'fstream' module.
The fstream module is just a shortened version of file stream (You must always use fstream). This module adds new commands and lines of code that do things with files, such as open them, edit them, close them, analyse them etc.
Let's call the modules.
To start this program off, we will need to call some modules. This time we will not just be calling the 'iostream' module, but we will also be calling the 'fstream' module.
The fstream module is just a shortened version of file stream (You must always use fstream). This module adds new commands and lines of code that do things with files, such as open them, edit them, close them, analyse them etc.
Let's call the modules.
In the previous tutorial about improving our programs, we found out that we must only declare variables in the loops they will be used in. We also looked at how global variables and local variables are declared. Since we are using local variables, we will not need to declare them now. Let's create the main loop
Now we can declare the variables we are using. Here are the variables we are using:
firstName, lastName, age and input.
All of these variables will be a string. As we have not declared that we will just be using the std namespace we will have to put std::string firstName, lastName, age, input;.
firstName, lastName, age and input.
All of these variables will be a string. As we have not declared that we will just be using the std namespace we will have to put std::string firstName, lastName, age, input;.
After we do this, we will be writing a new command. However, we must do a few things beforehand. What we need to do is create a file. This file will be a very small database with only 4 pieces of data. This file will also be a .txt file as they are much more easier to read into C++ than a rich text file (.rtf).
To make the file, open up a text editor. For Mac users, use TextEdit. For Windows users, use NotePad.
Once you have opened up your text editor, we have to create entries of data. These entries of data will be structured like this:
firstName, lastName, age,
For example. If the firstName was Laura, the lastName was Richardson, and the age was 23, the entry would look like this:
Laura, Richardson, 23,
The commas must be used to separate each piece of data. We are doing this so our program can read in the data more easily.
Here is an example database:
To make the file, open up a text editor. For Mac users, use TextEdit. For Windows users, use NotePad.
Once you have opened up your text editor, we have to create entries of data. These entries of data will be structured like this:
firstName, lastName, age,
For example. If the firstName was Laura, the lastName was Richardson, and the age was 23, the entry would look like this:
Laura, Richardson, 23,
The commas must be used to separate each piece of data. We are doing this so our program can read in the data more easily.
Here is an example database:
This database has 4 entries in it. You can use the same database if you want to.
Once you have made your database, we can continue.
Our next line of code will import this file into our program. It will also give the file a name so we do not have to keep writing its directory over and over and over again.
There is a way to get your databases directory into your code editor very easily. Do not add it in yet however until we have written the next piece of code.
Once you have made your database, we can continue.
Our next line of code will import this file into our program. It will also give the file a name so we do not have to keep writing its directory over and over and over again.
There is a way to get your databases directory into your code editor very easily. Do not add it in yet however until we have written the next piece of code.
This line of code will import our file. Where you see the directory is where you must put your files directory. To put it there more easily however, you can just drag your file to where you want its directory to be. If you cannot do this, then you will have to find your files directory, copy it, and paste it to where the directory is. Make sure the directory has speech marks around it otherwise the compiler will not be able to import your file.
Also, the std is being used in this line of code as this function is also under the standard namespace.
Once you have imported your file, we will start the main loop that will output the database to the command line. To do this, write the following code:
Also, the std is being used in this line of code as this function is also under the standard namespace.
Once you have imported your file, we will start the main loop that will output the database to the command line. To do this, write the following code:
The new piece of code we have added is a while loop. What does it mean however by file.good()? When we are going through the file, the status of the stream is reported as either 'good' or 'bad'. The file.good() piece of code is getting this status and is telling us whether the stream's status is 'good'. If not, the while loop would stop.
The stream would only be 'good' if it is able to collect data from the file it is looking at. If it is not able to get any data (which would be when it is at the end of the list/file), it would report its status as 'bad' or something that is not equal to 'good'.
So, knowing this, we can see that this while loop will only run until the program cannot get anymore data.
Now what is getline? Well, the getline piece of code in this loop is collecting data from the file. This code is structured as:
getline(where it is getting the data from, where it is going to store this collected data, the seperator (if there is a seperator then this part is needed. We need this part because the seperator in our database is a comma))
When the getline piece of code knows what the seperator is it can move through the file without having an iterator. An iterator is basically a variable that points at something. It is like a guide to the program. An iterator can tell the program to look at all sorts of things. For example: an item in a list, a number etc.
So we now understand the new code we have added. Lets move on to outputting this data to the command line. This outputting section is much more simple compared to what we looked at before as we will be using the cout command. Let's add the new code in.
The stream would only be 'good' if it is able to collect data from the file it is looking at. If it is not able to get any data (which would be when it is at the end of the list/file), it would report its status as 'bad' or something that is not equal to 'good'.
So, knowing this, we can see that this while loop will only run until the program cannot get anymore data.
Now what is getline? Well, the getline piece of code in this loop is collecting data from the file. This code is structured as:
getline(where it is getting the data from, where it is going to store this collected data, the seperator (if there is a seperator then this part is needed. We need this part because the seperator in our database is a comma))
When the getline piece of code knows what the seperator is it can move through the file without having an iterator. An iterator is basically a variable that points at something. It is like a guide to the program. An iterator can tell the program to look at all sorts of things. For example: an item in a list, a number etc.
So we now understand the new code we have added. Lets move on to outputting this data to the command line. This outputting section is much more simple compared to what we looked at before as we will be using the cout command. Let's add the new code in.
This is simply just outputting the data to the command line. You may of noticed how there is a simple space used in one of the strings of text. This space is used to seperate the variables from the strings of text. If we did not have that space, the program would output the wrong thing. Let's look at an example. firstName is equal to "Franklin" and the lastName is equal to "Freeman". The output would be:
FranklinFreeman
Instead we want:
Franklin Freeman
Because of this we need the space.
Well done, you have just finished making a simple database program. In the next tutorial, we will look at how to search through a database. (The next tutorial is just going to be an extension to this one so that tutorial might be a bit short).
FranklinFreeman
Instead we want:
Franklin Freeman
Because of this we need the space.
Well done, you have just finished making a simple database program. In the next tutorial, we will look at how to search through a database. (The next tutorial is just going to be an extension to this one so that tutorial might be a bit short).