Useful Utilities
In this article, we discuss useful utilities that you can use to convert, check and manipulate common data types in SplashKit programs.
Written by: Richard Denton and others
Last updated: October 2024
SplashKit’s Utilities library provides a range of useful functions that can assist you with converting, checking, and manipulating common data types in your SplashKit program.
These functions are useful in many areas of programming.
Before getting started with these utilities functions, it is important to be able to get inputs from the user using the command line (terminal).
Reading Text from the Command Line
Section titled “Reading Text from the Command Line”Reading text input from the user is straightforward for terminal-based applications.
In this context, Read Line allows you to read a line of text entered at the Terminal. This is a blocking function call, meaning it waits for the user to input the line before it returns. For command line applications, this is fine; you want it to wait for the input.
The following code demonstrates the use of Read Line in order to read in and display the user’s name.
#include "splashkit.h"
int main(){ string name; // declare a variable to store the name string quest; // and another to store a quest
write("What is your name: "); // prompt the user for input name = read_line(); // read user input
// read in another value write("And what is your quest? "); quest = read_line();
write_line(name + "'s quest is: " + quest); // output quest to the terminal
return 0;}
using static SplashKitSDK.SplashKit;
string name; // declare a variable to store the namestring quest; // and another to store a quest
Write("What is your name: "); // prompt the user for inputname = ReadLine(); // read user input
// Read in another valueWrite("And what is your quest? ");quest = ReadLine();
WriteLine(name + "'s quest is: " + quest); // output quest to the terminal
using SplashKitSDK;
namespace ReadingText{ public class Program { public static void Main() { string name; // declare a variable to store the name string quest; // and another to store a quest
SplashKit.Write("What is your name: "); // prompt the user for input name = SplashKit.ReadLine(); // read user input
// Read in another value SplashKit.Write("And what is your quest? "); quest = SplashKit.ReadLine();
SplashKit.WriteLine(name + "'s quest is: " + quest); // output quest to the terminal } }}
from splashkit import *
write("What is your name: ") # prompt the user for inputname = read_line() # read user input and store in a variable
# Read in another valuewrite("And what is your quest? ")quest = read_line()
write_line(name + "'s quest is: " + quest)
Converting a string to a number
Section titled “Converting a string to a number”SplashKit provides two useful functions for handling the conversion of a string
to either an int
or a double
.
#include "splashkit.h"
int main(){ // Convert the string "2017" to an integer 2017 string some_string = "2017"; int some_number = convert_to_integer(some_string);
write("The value of some_number is: "); write_line(some_number);
// Convert the string "3.14159265358979" to a double ~3.141593 string pi_string = "3.14159265358979"; double pi = convert_to_double(pi_string);
write("The value of pi is: "); write_line(pi);
return 0;}
using static SplashKitSDK.SplashKit;
//Convert the string "2017" to an integer 2017string some_string = "2017";int some_number = ConvertToInteger(some_string);
Write("The value of some_number is: ");WriteLine(some_number);
//Convert the string "3.14159265358979" to a double ~3.141593string pi_string = "3.14159265358979";double pi = ConvertToDouble(pi_string);
Write("The value of pi is: ");WriteLine(pi);
using SplashKitSDK;
namespace UsefulUtilities{ public class Program { public static void Main() { //Convert the string "2017" to an integer 2017 string some_string = "2017"; int some_number = SplashKit.ConvertToInteger(some_string);
SplashKit.Write("The value of some_number is: "); SplashKit.WriteLine(some_number);
//Convert the string "3.14159265358979" to a double ~3.141593 string pi_string = "3.14159265358979"; double pi = SplashKit.ConvertToDouble(pi_string);
SplashKit.Write("The value of pi is: "); SplashKit.WriteLine(pi); } }}
from splashkit import *
## Convert the string "2017" to an integer 2017some_string = "2017"some_number = convert_to_integer(some_string)
write("The value of some_number is: ")write_line_int(some_number)
## Convert the string "3.14159265358979" to a double ~3.141593pi_string = "3.14159265358979"pi = convert_to_double(pi_string)
write("The value of pi is: ")write_line_double(pi)
Usage scenario: Converting user input to an integer
Section titled “Usage scenario: Converting user input to an integer”Consider you want to write a program that accepts two numbers as input from a user, multiplies them, and outputs the result. One way to accomplish this is to use SplashKit’s Convert To Integer function.
#include "splashkit.h"
int main(){ // User input will initially be stored in these two strings. string snum_1, snum_2;
// And then it will be converted and stored in these integers. int inum_1, inum_2, result;
// Read user input write("Enter first number: "); snum_1 = read_line(); write("Enter second number: "); snum_2 = read_line();
// Convert user input to integers inum_1 = convert_to_integer(snum_1); inum_2 = convert_to_integer(snum_2);
// Compute the result result = inum_1 * inum_2;
// Output the results write(snum_1 + " multiplied by " + snum_2 + " equals "); write_line(result);
return 0;}
The same can be achieved for decimal numbers, simply by swapping convert_to_integer
with convert_to_double
, and using the appropriate data types.
//...
//Don't use integers, instead use doublesdouble inum_1, inum_2, result;
//...
inum_1 = convert_to_double(snum_1);inum_2 = convert_to_double(snum_2);
//...
using static SplashKitSDK.SplashKit;
//User input will initially be stored in these two strings.string snum_1, snum_2;
// And then it will be converted and stored in these integers.int inum_1, inum_2, result;
//Read user inputWrite("Enter first number: ");snum_1 = ReadLine();Write("Enter second number: ");snum_2 = ReadLine();
//Convert user input to integersinum_1 = ConvertToInteger(snum_1);inum_2 = ConvertToInteger(snum_2);
//Compute the resultresult = inum_1 * inum_2;
//Output the resultsWrite(snum_1 + " multiplied by " + snum_2 + " equals ");WriteLine(result);
The same can be achieved for decimal numbers, simply by swapping ConvertToInteger
with ConvertToDouble
, and using the appropriate data types.
//...
// Don't use integers, instead use doublesdouble inum_1, inum_2, result;
//...
inum_1 = ConvertToDouble(snum_1);inum_2 = ConvertToDouble(snum_2);
//...
using SplashKitSDK;
namespace UsefulUtilities{ public class Program { public static void Main() { //User input will initially be stored in these two strings. string snum_1, snum_2;
// And then it will be converted and stored in these integers. int inum_1, inum_2, result;
//Read user input SplashKit.Write("Enter first number: "); snum_1 = SplashKit.ReadLine(); SplashKit.Write("Enter second number: "); snum_2 = SplashKit.ReadLine();
//Convert user input to integers inum_1 = SplashKit.ConvertToInteger(snum_1); inum_2 = SplashKit.ConvertToInteger(snum_2);
//Compute the result result = inum_1 * inum_2;
//Output the results SplashKit.Write(snum_1 + " multiplied by " + snum_2 + " equals "); SplashKit.WriteLine(result); } }}
The same can be achieved for decimal numbers, simply by swapping SplashKit.ConvertToInteger
with SplashKit.ConvertToDouble
, and using the appropriate data types.
//...
// Don't use integers, instead use doublesdouble inum_1, inum_2, result;
//...
inum_1 = SplashKit.ConvertToDouble(snum_1);inum_2 = SplashKit.ConvertToDouble(snum_2);
//...
from splashkit import *
# Read user inputwrite("Enter first number: ")snum_1 = read_line()write("Enter second number: ")snum_2 = read_line()
# Convert user inputs to integersinum_1 = convert_to_integer(snum_1)inum_2 = convert_to_integer(snum_2)
# Compute the resultresult = inum_1 * inum_2
# Output the resultwrite(snum_1 + " multiplied by " + snum_2 + " equals ")write_line_int(result)
The same can be achieved for decimal numbers, simply by swapping convert_to_integer
with convert_to_double
, and using the appropriate data types.
#...
# Don't uses integers, instead use doublesinum_1 = convert_to_double(snum_1)inum_2 = convert_to_double(snum_2)
#...
Checking if a string is a number
Section titled “Checking if a string is a number”Another helpful set of functions provided by SplashKit’s utilities library is Is Integer and Is Number.
Consider the following.
#include "splashkit.h"
int main(){ string message_1 = "9781273"; string message_2 = "23129739.13"; string message_3 = "Hello world.";
if (is_integer(message_1)) write_line("Message 1 contains an integer!");
if (is_number(message_1)) write_line("Message 1 contains a number!");
if (not is_integer(message_2)) write_line("Message 2 is not an integer!");
if (is_number(message_2)) write_line("Message 2 contains a number!");
if (not is_integer(message_3)) write_line("Message 3 is not an integer!");
if (not is_number(message_3)) write_line("Message 3 is not a number!");
return 0;}
using static SplashKitSDK.SplashKit;
string message_1 = "9781273";string message_2 = "23129739.13";string message_3 = "Hello world.";
if (IsInteger(message_1)) WriteLine("Message 1 contains an integer!");
if (IsNumber(message_1)) WriteLine("Message 1 contains a number!");
if (!IsInteger(message_2)) WriteLine("Message 2 is not an integer!");
if (IsNumber(message_2)) WriteLine("Message 2 contains a number!");
if (!IsInteger(message_3)) WriteLine("Message 3 is not an integer!");
if (!IsNumber(message_3)) WriteLine("Message 3 is not a number!");
using SplashKitSDK;
namespace UsefulUtilities{ public class Program { public static void Main() { string message_1 = "9781273"; string message_2 = "23129739.13"; string message_3 = "Hello world.";
if (SplashKit.IsInteger(message_1)) SplashKit.WriteLine("Message 1 contains an integer!");
if (SplashKit.IsNumber(message_1)) SplashKit.WriteLine("Message 1 contains a number!");
if (!SplashKit.IsInteger(message_2)) SplashKit.WriteLine("Message 2 is not an integer!");
if (SplashKit.IsNumber(message_2)) SplashKit.WriteLine("Message 2 contains a number!");
if (!SplashKit.IsInteger(message_3)) SplashKit.WriteLine("Message 3 is not an integer!");
if (!SplashKit.IsNumber(message_3)) SplashKit.WriteLine("Message 3 is not a number!"); } }}
from splashkit import *
message_1 = "9781273"message_2 = "23129739.13"message_3 = "Hello world."
if is_integer(message_1): write_line("Message 1 contains an integer!")
if is_number(message_1): write_line("Message 1 contains a number!")
if not is_integer(message_2): write_line("Message 2 is not an integer!")
if is_number(message_2): write_line("Message 2 contains a number!")
if not is_integer(message_3): write_line("Message 3 is not an integer!")
if not is_number(message_3): write_line("Message 3 is not a number!")
The terminal output should be:
Message 1 contains an integer!Message 1 contains a number!Message 2 is not an integer!Message 2 contains a number!Message 3 is not an integer!Message 3 is not a number!
Usage scenario: accept user input as a number
Section titled “Usage scenario: accept user input as a number”SplashKit’s Is Integer and Is Number functions can be used to validate user input, ensuring the user always enters a valid data type.
#include "splashkit.h"#include <string>
using namespace std;
/** * Reads input from a user, only allowing whole numbers. * @prompt string - The string to display to the user. * @return int */int read_integer(string prompt){
// Prompt the user with the message write(prompt);
// Read the user input as a string. string line = read_line();
// Check if user input is a valid whole number, loop until it is. while (!is_integer(line)) { write_line("Please enter a whole number.");
write(prompt); line = read_line(); }
// Convert the user input to an integer before returning it. return convert_to_integer(line);}
int main(){ int height;
height = read_integer("Enter your height in centimetres: "); write("You are "); write(height); write_line("CM tall!");
return 0;}
using static SplashKitSDK.SplashKit;
static int ReadInteger(string prompt){ // Prompt the user with the message Write(prompt);
// Read the user input as a string. string line = ReadLine();
// Loop while the user's input is NOT a valid whole number. while (!IsInteger(line)) { // If the user input was not a valid whole number, ask them to enter a whole number. WriteLine("Please enter a whole number."); Write(prompt); line = ReadLine(); } // Convert the user input to an integer before returning it. return ConvertToInteger(line);}
// Start of "main" codeint height;
height = ReadInteger("Enter your height in centimetres: ");Write("You are ");Write(height);WriteLine("cm tall!");
using SplashKitSDK;
namespace UsefulUtilities{ public class Program { public static int ReadInteger(string prompt) { // Prompt the user with the message SplashKit.Write(prompt);
// Read the user input as a string. string line = SplashKit.ReadLine();
// Loop while the user's input is NOT a valid whole number. while (!SplashKit.IsInteger(line)) { // If the user input was not a valid whole number, ask them to enter a whole number. SplashKit.WriteLine("Please enter a whole number."); SplashKit.Write(prompt); line = SplashKit.ReadLine(); } // Convert the user input to an integer before returning it. return SplashKit.ConvertToInteger(line); }
public static void Main() { int height;
height = ReadInteger("Enter your height in centimetres: "); SplashKit.Write("You are "); SplashKit.Write(height); SplashKit.WriteLine("cm tall!"); } }}
from splashkit import *
def read_integer(prompt): # Prompt the user with the message write(prompt)
# Read the user input as a string. line = read_line()
# Loop while the user's input is NOT a valid whole number. while not is_integer(line):
# If the user input was not a valid whole number, ask them to enter a whole number. write_line("Please enter a valid whole number.") write(prompt) line = read_line()
# Convert the user input to an integer before returning it. return convert_to_integer(line)
# Start of "main" codeheight = read_integer("Enter your height in centimetres: ")
write("You are ")write_int(height)write_line("cm tall!")
Manipulating strings
Section titled “Manipulating strings”In addition to the functionality provided by each language’s standard library (The string
library in C++, and the System
library in C#), SplashKit’s utilities library provides some extra string manipulation functions that can assist you with manipulating string
data.
#include "splashkit.h"
int main(){ string name = "Richard"; string location = " Burwood";
// Convert "Richard" to "RICHARD" name = to_uppercase(name); write_line(name);
// Convert "RICHARD" to "richard" name = to_lowercase(name); write_line(name);
// Remove all of the empty spaces at the start of " Burwood". write_line("Before: " + location); location = trim(location); write_line("After: " + location);
return 0;}
using static SplashKitSDK.SplashKit;
string name = "Richard";string location = " Burwood";
//Convert "Richard" to "RICHARD"name = ToUppercase(name);WriteLine(name);
//Convert "RICHARD" to "richard"name = ToLowercase(name);WriteLine(name);
//Remove all of the empty spaces at the start of " Burwood".WriteLine("Before: " + location);location = Trim(location);WriteLine("After: " + location);
using SplashKitSDK;
namespace UsefulUtilities{ public class Program { public static void Main() { string name = "Richard"; string location = " Burwood";
//Convert "Richard" to "RICHARD" name = SplashKit.ToUppercase(name); SplashKit.WriteLine(name);
//Convert "RICHARD" to "richard" name = SplashKit.ToLowercase(name); SplashKit.WriteLine(name);
//Remove all of the empty spaces at the start of " Burwood". SplashKit.WriteLine("Before: " + location); location = SplashKit.Trim(location); SplashKit.WriteLine("After: " + location); } }}
from splashkit import *
name = "Richard"location = " Burwood"
# Convert "Richard" to "RICHARD"name = to_uppercase(name)write_line(name)
# Convert "RICHARD" to "richard"name = to_lowercase(name)write_line(name)
# Remove all of the empty spaces at the start of " Burwood".write_line("Before: " + location)location = trim(location)write_line("After: " + location)