Skip to content

Explore procedures

To get started with structuring our code, let’s create a couple of useful procedures. We can build some small utilities that may be useful elsewhere, and create a test programs to explore how they work.

Let’s start by creating a procedure to print a line of ’-’ characters to the Terminal. This could be useful when we create some terminal programs later, and we want to output something like a menu. The menu could have section separated by these lines.

How long should the line be?

Great question. We don’t have to have this set. Instead, we could add a length parameter to allow the caller to say how long the line should be.

The following table provides an overview of the desired functionality of our print line procedure

ProcedurePrint Line
Parameterslength: The number of characters to print. Represents the length of the line.
DescriptionWrites a number of ‘-’ characters to the Terminal. The number of characters to write is specified in the length parameter.
Specification for the print line procedure

The flowchart and pseudocode for this procedure are shown below.

Flowchart for the logic in Print Line code

Flowchart for the logic in print line code, with blocks highlighted

Procedure: Print Line
-------------------------------
Parameters:
- Length (Integer)
Local Variables:
- i (Integer)
Steps
Assign i the value 0
While i < length
Output '-'
Increase i by 1
Output a new line
Pseudocode for Print Line

Write Code and Test

We can code this up in a small test program. The following is a good start. The main function tests printing a few lines, and then gets the user to enter a length and uses that to print a final line. Have a go at this yourself, and refer to the code below if you get stuck.

#include "splashkit.h"
// Add print line code here...
int main()
{
string input;
int test_length;
print_line(20);
write_line("| Line print test |");
print_line(20);
write("Enter a length for a test line: ");
input = read_line();
test_length = stoi(input);
print_line(test_length);
}
  • /**
    * Print a line onto the Terminal.
    *
    * @param length the length of the line to print
    */
    void print_line(int length)
    {
    int i = 0;
    while (i < length)
    {
    write("-");
    i++;
    }
    write_line("\n");
    }

Generalising Further

If we think about this, we could create a more general procedure that prints any character, or string, a number of times to the terminal. This could then be used to print repeated patterns or repeated text when needed, and we could use it to implement the print line procedure.

The following table provides an overview of a print repeated procedure. Have a go at implementing this, and using it to simplify the code in your print_line procedure.

ProcedurePrint Repeated
Parameterstext: The text to repeat (a string).
times: The number of times to print this.
with_newline: A boolean to indicate if a new line should be added to the end.
DescriptionWrites the text to the Terminal a given number of times, adding a new line when with_newline is true.

Here is our updated test program - notice we want to keep print line, but you should be able to simplify it to a single line when you are done creating print repeated.

#include "splashkit.h"
// Add print repeated here...
// Add print line code here...
int main()
{
string input;
int test_length;
print_line(20);
write_line("| Line print test |");
print_line(20);
print_repeated("--+--+", 5, true);
print_repeated("Hello World\n", 5, false);
print_repeated("--+--+", 5, true);
write("Enter a length for a test line: ");
input = read_line();
test_length = stoi(input);
print_line(test_length);
}
  • /**
    * Print text to the terminal a number of times, allowing repeated patterns.
    *
    * @param text the text to repeat printing
    * @param times the number of times to repeat the text
    * @param with_newline when true, this adds a newline to the end of the output
    */
    void print_repeated(string text, int times, bool with_newline)
    {
    int i = 0;
    while (i < times)
    {
    write(text);
    i++;
    }
    if(with_newline)
    {
    write_line("\n");
    }
    }
    /**
    * Print a line onto the Terminal.
    *
    * @param length the length of the line to print
    */
    void print_line(int length)
    {
    print_repeated("-", length, true);
    }

Test it out

See if you can create a print menu header. This could print a heading to the terminal in a box. For example, this could appear something like one of these options:

---------
Heading
---------
+---------+
| Heading |
+---------+