Implementing the date verification algorithm

Date verification lab taskExtra: Stream redirection on Windows

Task: create a program that can verify correct dates and give an error if the the date is invalid.

Requirements for base task

  • After each date, it should specify if it was valid or not
  • Valid years: 1900 – 2099
  • Leap year check
  • Must be able to verify 100 dates in a single run
  • The input data comes from the input stream which has been redirected to take it from a file. The dates that you need to check for are given in this document.
  • Date format: DDMMYYYY
  • During the base task, all the functions must be created as they are described in the base code!
  • When showing the program, you must use stream redirection.

Advanced

  • Task 1: Give a reason why the date failed verification (e.g. too many days for leap year February, year out of range)
  • Task 2: Allow at least two formats
    e.g. [DDMMYYYY], [DD.MM.YYYY], [mm/dd/yyyy], [dd. mmm yyyy]

https://en.wikipedia.org/wiki/Date_format_by_country

Algorithm

The algorithm is showing a higher level view of the solution and is given for you as a guideline to solve the task. It doesn’t show the information exchange between functions. Due to separating the code out to functions you will notice that also the order of some operations will change as well as some logic will be written differently.

Steps needed

To start off, we should create a text file that will contain the data we would have otherwise typed in from the keyboard. When writing the data file we should make sure that all of the different cases would be covered that may occur.  For some tasks, we will require multiple files with different input. In this case, we can have a single file with multiple combinations in it.

When creating your own tests in the future, make sure to cover all the cases that might occur. In this case it would mean testing both valid and invalid cases.  E.g. 11th of December 1981 would be a normal valid one, however 51st of December, month number 22 etc. will would not. In addition, you need to take extra care to test the edge cases – e.g. months 0, 1, 12, 13. Also testing days and months combined, e.g. 31st of December and 32nd of December. In addition, since these are dates, we also need to check for dates on and off leap years – more precisely 29th of February on and off a leap year.

This is your test input. Save it as a text file to the same directory as your source code and program! You’re allowed to add your own lines to the end.

Next you’ll need to make a program that will start to verify these dates. We are providing you with a starter code where you can start from: kp_base.c

The starter code contains all the prototypes and comments for all the required functions. This is also what you will need to solve for during the base task. When doing advanced tasks, some functions may need to be altered slightly, however the parameters and return values should most likely remain the same, however meaning should change a bit.

First two functions will be done during the class. If you weren’t in the lab, check it out from the video and/or from gitlab!

From this point forward, we should have 2 files:

  1. Program code (e.g. kp_base.c )
  2. Text file with the dates (e.g. input )

Next up we need to compile the program. One option would be to use Geany or any other IDE that you’ve used so far. You can do that if that’s what you feel the most comfortable with. Just don’t forget to recompile after changing the code!

As an alternative, you can try to compile the code from the command line (that’s basically what happens when you press the build button anyway). The structure to do it yourself is compiler_name -o program_name source_code_file.cIt is important that -o is followed by the name of the desired program (output). In the end of the line we put the name of the source code file that we wrote. In addition we will also use flags to make sure that all the additional warnings are displayed – e.g. -Wall and -Wextra.

Now lets put all this together. Once you are in the right directory with your command like, you can enter the following line:

gcc -Wall -Wextra -o kp kp_base.c

Using this like, the source code file kp_base.c will be compiled into a program called kp.

Just to verify the that you have everything in place

Before we had 2 files, now we should have 3 of them:

  • kp – the so called binary file a.k.a. the program that I can execute (the file I got from compiling my source code)
  • kp.c – the source code I used to compile my executable
  • input – text file, that contains my test data.

Now we run the program so that instead of writing the input from the keyboard, it will be read from the file that we’ve composed (using the stream redirection). For this, use the following line: ./programname < datafile

./programname – this is how you can run a program in Linux console
< – a symbol that will redirect my input stream from keyboard to a file specified after it
datafile – the redirected input will be read from this file instead of the keyboard

After implementing the date verification, Your output might look something like this:
 

If You’re going for advanced implementation, try validating the input before checking

The following example is how to do stream redirection in Windows. In the case of Linux it’s mostly the same with some alterations like the programs not having .exe extensions and to execute a program, you need to start it with “./”, indicating that you are executing something from the current directory.

1. Make sure that all the files are where required

At minimum, we’ll need 2 files. For the sake of simplicity, we should put them in the same folder.

  • The program (NB! Source code is not a program! A program is a binary file that we get from compiling the source code).
  • Data file (text file that contains data we would have otherwise typed on the keyboard).

Most likely however you will have more files – e.g. the .c file where our source code is written. You might also notice a .o object file.

1.1 Sample files

First, Your input data file that we will stream into the program (e.g. input.txt)

Secondly, your program code that reads the input and typically would do something with it. In this case, we just print it back out.

1.2 Compile the program if you haven’t done this yet

You can either compile from your preferred code editor or through the command line. It doesn’t matter which option you choose. Just make sure to recompile every time you change the source code!

2. Open a command line. The current working directory of your command line must be to the folder where you have all the files previously mentioned. There are multiple options to do so. I’m going to show you the 2 simplest ones.

2a Activate your address bar, write “cmd” and hit enter.

 

2b. Hold down the shift key on your keyboard. In the file explorer, right-click your mouse on an area where there are no items (just white background). Choose “Open command window here”. NB! Since Windows 10 version 14986, the default opens PowerShell. Even though it is possible to use it, it is a bit different and it won’t be covered here.

3. Execute the program using stream redirection.

Generic structure: program.exe < input.txt

First is the name of the program, then it indicates that the input stream will be redirected and lastly it specifies from which file it comes from.

In the case of my folder structure, I need to execute: 7_base_read_matrix.exe < input.txt

Just to make double sure, you can follow the same process in a short video below. I’m also using the command dir to show you the files in my folder, however you do not need to do that. It’s primarily informative.

NB! When running a program through stream redirection, you will not see the redirected streams. If you wish to observe it, you may need additional print statements.

Common issue: You need to execute the program (.exe in windows), not the source code (.c).

Some tips and tricks:

  • Use autocomplete. To write a name of a file (program, source, data, …) or a folder, write the first 2-3 characters and hit the tab key.
  • Use the command history to save time. Leave the command window open. Once you have updated the source code and recompiled, just hit the up arrow key and you will go through the history of previous commands.
  • Create multiple test files. This way you can easily test a vast set of different input combinations. Want to automate that? Create a script to do so.